我正在开发一个应用程序,我必须在其中连接到 Android 4.3 上的蓝牙设备。
getConnectedDevices()
在使用 by 连接 BLE 设备后,我想使用下面的代码来获取连接的设备mBluetoothGatt.connect()
,但无法正常工作。
@Override
public void onServiceDisconnected(int profile) {
// TODO Auto-generated method stub
Log.v(TAG, "profile1" + profile);
Log.v(TAG, "BluetoothProfile.HEADSET1 = " + BluetoothProfile.HEADSET);
if(profile == BluetoothProfile.HEADSET) {
mBluetoothHeadset = null;
}
}
@Override
public void onServiceConnected(int profile, BluetoothProfile proxy) {
// TODO Auto-generated method stub
Log.v(TAG, "profile2 " + profile);
Log.v(TAG, "BluetoothProfile.HEADSET2 = " + BluetoothProfile.HEADSET);
if(profile == BluetoothProfile.HEADSET) {
mBluetoothHeadset = (BluetoothHeadset) proxy;
Log.v(TAG, "mBluetoothHeadset = " + mBluetoothHeadset);
List<BluetoothDevice> devicelist = mBluetoothHeadset.getConnectedDevices();
Log.v(TAG, "mBluetoothHeadset.getConnectedDevices = " + mBluetoothHeadset.getConnectedDevices());
Log.v(TAG, "devicelist = " + devicelist);
for(BluetoothDevice dev : devicelist) {
Log.v(TAG, "connectedDevices = " + dev);
}
}
}
};
@Override
protected void onNewIntent(Intent intent) {
// TODO Auto-generated method stub
super.onNewIntent(intent);
setIntent(intent);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mBluetoothAdapter.getProfileProxy(Main.this, mProfileListener, BluetoothProfile.HEADSET);
}
从日志中可以看出,它没有调用函数 getConnectedDevices(); 日志是:
D/BluetoothHeadset( 3454): Proxy object connected
V/Main ( 3454): profile2 = 1
V/Main ( 3454): BluetoothProfile.HEADSET2 = 1
V/Main ( 3454): mBluetoothHeadset = android.bluetooth.BluetoothHeadset@4a99cddc
V/Main ( 3454): mBluetoothHeadset.getConnectedDevices = []
V/Main ( 3454): devicelist = []
已连接列表为空,我确定我有一个已连接的 BLE 设备。我的代码有问题吗?