我目前正在研究一种通过蓝牙连接到仪器的Android应用程序,需要编写字符串命令并接收字符串响应.目前我通过Wi-Fi进行TCP/IP连接/读/写工作,现在尝试实现蓝牙.但我遇到了一些障碍.我一直在网上搜索试图找到类似的东西的例子,并没有运气.我一直在使用Android开发人员资源示例:蓝牙聊天作为我的主要参考点.
我当前的代码似乎工作..然后它会在连接点抛出Service Discovery Failed异常.我正在使用DeviceListActivity该类来发现和选择我想要连接的设备.它返回anActivityResult,然后我的蓝牙类等待它处理它,然后连接到它.下面的代码几乎与蓝牙聊天应用程序相同.
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(!m_BluetoothAdapter.isEnabled())
{
m_BluetoothAdapter.enable();
}
switch (requestCode) {
case REQUEST_CONNECT_DEVICE:
// When DeviceListActivity returns with a device to connect
if (resultCode == Activity.RESULT_OK) {
// Get the device MAC address
String address = data.getExtras()
.getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
// Get the BLuetoothDevice object
BluetoothDevice device = m_BluetoothAdapter.getRemoteDevice(address);
// Attempt to connect to the device
connect(device);
}
break;
case REQUEST_ENABLE_BT:
// When the request to enable Bluetooth returns
if (resultCode …Run Code Online (Sandbox Code Playgroud) 有没有人知道如何在API级别2.3.3的2个Android设备之间创建不安全的RFCOMM连接,同时使用任意声明的服务名称?(不是随机或更改服务名称,只是我自己定义的服务名称)
我试图在2个Android设备之间创建一个不安全的Rfcomm连接:Droid X2和华硕Transformer.我假设这两款设备都具有Android 2.3.3级别的功能,实际上可以使用不安全的Rfcomm.
当我尝试按照此处所述创建蓝牙连接时,使用now public createInsecureRfcommSocketToServiceRecord()和listenUsingInsecureRfcommWithServiceRecord(SERVICE,UUID),我得到一个报告:
java.io.IOException: Service discovery failed
at android.bluetooth.BluetoothSocket$SdpHelper.doSdp(BluetoothSocket.java:377)
at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:201)
at com.s4d.bluenomad.DeviceConnections$ClientThread.run(DeviceConnections.java:406)
Run Code Online (Sandbox Code Playgroud)
我发现了一个相关的问题,即有人创建正常连接时会收到此错误并使用反射来调用私有方法.但是,我不知道现在的私有方法会对应于启动"不安全"连接.我尝试使用相关问题中提出的解决方案,但Android要求我配对设备,这正是我需要避免的.我确实需要不安全的方法.
我甚至尝试了这里概述的官方和黑客解决方案的组合
创建ServerThread以侦听连接
Log.i(TAG, "Constructing a ServerThread");
// Use a temporary object that is later assigned to serverSocket,
// because serverSocket is final
BluetoothServerSocket tmp = null;
try {
// MY_UUID is the app's UUID string, also used by the client code
tmp = btAdapter.listenUsingInsecureRfcommWithServiceRecord(SERVICE_NAME, SERVICE_UUID);
Log.i(TAG,"Started listening on insecure RFCOMM channel for service …Run Code Online (Sandbox Code Playgroud) 我正在编写一个应用程序,它将平板电脑的字节代码发送到μ控制器.在联想A1(Androi 2.3)和三星Galaxy Tab 7 Plus N(Android 3.2)上一切正常.现在我遇到了新的三星Galaxy Tab 2(Android 4.0)的问题.
我能够配对蓝牙天线(连接到μ控制器并通过串行协议进行通信).当我启动应用程序时,我再次被要求输入密码并配对.输入配对密码后,我的主要布局可见,但未建立连接.
日食中的LogCat告诉我:
06-19 16:00:20.656: V/BluetoothSocket.cpp(3189): availableNative
06-19 16:00:20.664: V/BluetoothSocket.cpp(3189): abortNative
06-19 16:00:20.664: V/BluetoothSocket.cpp(3189): ...asocket_abort(49) complete
06-19 16:00:20.664: I/ActivityManager(185): No longer want com.google.android.partnersetup (pid 3220): hidden #16
06-19 16:00:20.671: V/BluetoothSocket.cpp(3189): availableNative
06-19 16:00:20.671: V/BluetoothSocket.cpp(3189): destroyNative
06-19 16:00:20.671: V/BluetoothSocket.cpp(3189): ...asocket_destroy(49) complete
06-19 16:00:20.679: D/KeyguardViewMediator(185): setHidden false
06-19 16:00:20.679: W/System.err(3189): java.io.IOException: socket closed
06-19 16:00:20.679: W/System.err(3189): at android.bluetooth.BluetoothSocket.available(BluetoothSocket.java:370)
06-19 16:00:20.679: W/System.err(3189): at android.bluetooth.BluetoothInputStream.available(BluetoothInputStream.java:40)
06-19 16:00:20.679: W/System.err(3189): at java.io.BufferedInputStream.available(BufferedInputStream.java:114)
06-19 16:00:20.687: W/System.err(3189): at ebs.alphadidact.control.ReceiveThread.run(ReceiveThread.java:79) …Run Code Online (Sandbox Code Playgroud) 我需要使用SPP 将KwikBlue-2模块开发套件连接到Nexus One上的Android BluetoothChat示例.在将BluetoothChat示例示例中的UUID更改为SPP设备的00001101-0000-1000-8000-00805F9B34FB 之后,我能够使用tera术语连接和交换串行数据(一次).然后我循环使用KwikBlue-2模块开发套件来验证连接是否可重复,但java.io.IOException: Connection refused在我尝试连接时不断获得
.有任何想法吗?
43.200: ERROR/BluetoothEventLoop.cpp(96): event_filter: Received signal org.bluez.Adapter:PropertyChanged from /org/bluez/219/hci0
52.200: ERROR/BluetoothChat(707): - ON PAUSE -
54.470: ERROR/BluetoothService.cpp(96): stopDiscoveryNative: D-Bus error in StopDiscovery: org.bluez.Error.Failed (Invalid discovery session)
54.520: ERROR/BluetoothChat(707): + ON RESUME +
54.530: ERROR/BluetoothService.cpp(96): stopDiscoveryNative: D-Bus error in StopDiscovery: org.bluez.Error.Failed (Invalid discovery session)
54.620: ERROR/BluetoothService.cpp(96): stopDiscoveryNative: D-Bus error in StopDiscovery: org.bluez.Error.Failed (Invalid discovery session)
55.520: ERROR/BluetoothEventLoop.cpp(96): event_filter: Received signal org.bluez.Device:PropertyChanged from /org/bluez/219/hci0/dev_00_60_57_FD_42_18
56.010: ERROR/BluetoothChatService(707): …Run Code Online (Sandbox Code Playgroud)