我目前正在研究一种通过蓝牙连接到仪器的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) 我已经发现了其他设备,我已经配对了它.至少我把它放在Android手机上的配对设备列表中.
现在在BluetoothSocket.connect()上可能会出现两个问题:
远程设备忘记了配对,因为它只能配对另一台设备,并且已与另一部手机配对
=>然后在一定的超时后连接失败.
是否可以检查已配对的设备是否真的可用,并记住它是否与我的手机配对而没有连接到它?这与检测设备是否已连接无关.配对和可见与连接不同.
我可以枚举配对Bluetooth设备,我需要在应用启动时检查它们当前的连接状态.
但是,我发现无法获得连接状态?!
以下是我尝试但失败的一些努力:
它有获得远程设备的键状态的方法,和用于键状态可能值如下:BOND_NONE,BOND_BONDING,BOND_BONDED.但这不是连接状态.
该方法可用于获取连接状态,但BluetoothProfile必须首先获取对象.
public abstract int getConnectionState (BluetoothDevice device);
Run Code Online (Sandbox Code Playgroud)
正如在doc中描述的那样:" 客户端应调用getProfileProxy(Context,BluetoothProfile.ServiceListener,int),以获取配置文件代理. ",它只能ServiceListener在状态发生变化时在被调用时进行检索.
如果我在启动时查询状态而没有发生任何状态更改,该怎么办?
它提供了一种方法:
public int getConnectionState (BluetoothDevice device, int profile);
Run Code Online (Sandbox Code Playgroud)
但是它不能被用于检测连接状态的的Bluetooth扬声器,作为配置文件参数只接受GATT或GATT_SERVER(用于低能量设备),其他轮廓(HEADSET,HEALTH,A2DP)不被支持.
它提供了一种方法:
public int getProfileConnectionState (int profile);
Run Code Online (Sandbox Code Playgroud)
此功能可用于检查本地Bluetooth适配器是否连接到特定配置文件的任何远程设备.它可用于检查指定的配置文件.但它不能用于检查给定的设备.
有谁知道如何获得给定Bluetooth设备的连接状态?
提前致谢.
我想知道我与设备的蓝牙连接何时断开连接.我发现这个要检查:
IntentFilter filter1 = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED);
IntentFilter filter2 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);
IntentFilter filter3 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED);
this.registerReceiver(mReceiver, filter1);
this.registerReceiver(mReceiver, filter2);
this.registerReceiver(mReceiver, filter3);
//The BroadcastReceiver that listens for bluetooth broadcasts
mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
//Device found
}
else if (BluetoothAdapter.ACTION_ACL_CONNECTED.equals(action)) {
//Device is now connected
}
else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
//Done searching
}
else if (BluetoothAdapter.ACTION_ACL_DISCONNECT_REQUESTED.equals(action)) {
//Device is about to …Run Code Online (Sandbox Code Playgroud)