如何检测连接到Android的蓝牙扫描仪

时间:2016-02-02 09:15:35

标签: android bluetooth

我想找到蓝牙扫描仪是否每7秒钟连接到我的Android手机。为此,我创建了一个receiver,在那里我找到了bondeddevices然后找到了它的主要类。但它没有找到任何设备。可能是什么问题?

这里是活动类:

public class Activity_Home extends ActionBarActivity implements   
OnClickListener {

CountDownTimer wifiTimer, timer;
BluetoothAdapter mBluetoothAdapter;
BluetoothDevice bluetoothDevice;
CheckBluetoothScanner mReceiver;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    mReceiver = new CheckBluetoothScanner();
    isScannerConnected();
}

private void isScannerConnected() {

    timer = new CountDownTimer(Long.MAX_VALUE, 7000) { // -------call
        // amethod
        // after
        // certain
        // time
        // interval-------

        // This is called every interval. (Every 7 seconds in this example)
        public void onTick(long millisUntilFinished) {

            Log.d("test", "Timer tick");
            System.out.println("=======bluetooth--start");
            try {
                mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

                if (mBluetoothAdapter.isEnabled()) {

                    IntentFilter filter = new IntentFilter(
                            BluetoothDevice.ACTION_FOUND);
                    registerReceiver(mReceiver, filter);
                    mBluetoothAdapter.startDiscovery();


                    System.out.println("============utility: "
                            + Utility.SCANNER_ON);
                    if (Utility.SCANNER_ON.equalsIgnoreCase("true")) {
                        tv_scanner
                                .setBackgroundResource(R.drawable.scanner);
                    } else {
                        tv_scanner.setBackgroundResource(0);
                    }
                } else {
                    tv_scanner.setBackgroundResource(0);
                }
            } catch (NullPointerException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        public void onFinish() {
            Log.d("test", "Timer last tick");

            timer.cancel();

        }
    }.start();

}

这是接收者类:

 public class CheckBluetoothScanner extends BroadcastReceiver {

public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();

    if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
        final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
                BluetoothAdapter.ERROR);

    }
    else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
        BluetoothDevice device = (BluetoothDevice) intent
                .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        Set pairedDevices = BluetoothAdapter.getDefaultAdapter()
                .getBondedDevices();
        BluetoothDevice device1 = null;
        for (Object item : pairedDevices) {
            device1 = (BluetoothDevice) item;
        }
        BluetoothClass bluetoothClass = device1.getBluetoothClass();
        System.out.println("======bluetoothClass"+bluetoothClass);

        if (bluetoothClass != null) {
            int deviceClass = bluetoothClass.getDeviceClass();

            System.out.println("=======deviceclass:"+deviceClass);
            if (deviceClass == Device.Major.IMAGING
                    || deviceClass == Device.Major.PERIPHERAL) {
                Utility.SCANNER_ON = "true";


        } else {
                    Utility.SCANNER_ON = "false";
                }
            }
        }
    }
   };

0 个答案:

没有答案