我正在使用一个搜索附近蓝牙的应用程序.我使用下面的代码,它在5.0中找到它返回附近的蓝牙设备,但是当我在6.0.1中测试时BluetoothDevice.ACTION_FOUND没有被调用,只有BluetoothAdapter.ACTION_DISCOVERY_STARTED和BluetoothAdapter.ACTION_DISCOVERY_FINISHED被调用.
@Override
protected void onCreate(Bundle savedInstanceState) {
BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
bluetoothAdapter = bluetoothManager.getAdapter();
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,0);
startActivity(discoverableIntent);
}
private final BroadcastReceiver bReciever = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
System.out.println(action);
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
System.out.println(device);
}
}
};
public void onSearch(View v){
if (bluetoothAdapter.isDiscovering()) {
bluetoothAdapter.cancelDiscovery();
}
System.out.println(bluetoothAdapter.startDiscovery());
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(bReciever, intentFilter);
}
Run Code Online (Sandbox Code Playgroud)
我的问题是为什么在6.0.1中没有调用BluetoothDevice.ACTION_FOUND?谢谢.
| 归档时间: |
|
| 查看次数: |
122 次 |
| 最近记录: |