我正在开发一个应用程序,我必须连接到Android 4.3上的蓝牙设备。
我希望在使用getConnectedDevices()
连接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设备。 我的代码中有什么问题吗?