在Android中,我们如何定期扫描蓝牙设备?

时间:2015-05-19 07:09:36

标签: android android-bluetooth

蓝牙扫描应该在找到所选设备后立即停止,然后在停止后应该再次启动,当我执行以下操作时,我会收到错误消息,说应用程序已停止。我想扫描3次,并存储他们的半径。

private BluetoothAdapter mBtAdapter;
mBtAdapter.startDiscovery();

//this string stores the MAC Address of the required device
string name = "12:34:D3:s3:we";

private final BroadcastReceiver mReceiver = new BroadcastReceiver()
{
        @Override
        public void onReceive(Context context, Intent intent)
        {
            String action = intent.getAction();

            // When discovery finds a device
            if (BluetoothDevice.ACTION_FOUND.equals(action))
            {
                 if(device.getAddress.equals(name)){
                     mBtAdapter.cancelDiscovery();
                 }

            }

            else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action))
            {
                mBtAdapter.startDiscovery();

            }
        }
}

0 个答案:

没有答案