我目前正在研究一种通过蓝牙连接到仪器的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)