通过蓝牙伪装设备。 createBond()

时间:2017-12-25 19:14:33

标签: android android-studio

我有问题。我试图将设备与蓝牙配对,但并不总是显示配对对话框 有时日志显示尝试createBond()后直接进入BluetoothDevice.BOND_NONE。

//import 
public class MainActivity extends AppCompatActivity {
   .... define variable

    private BroadcastReceiver discoveryDeviceReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
              mBTDevices = new ArrayList<BluetoothDevice>();
            final String action = intent.getAction();
            if (action.equals(BluetoothDevice.ACTION_FOUND)) {
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                mBTDevices.add(device);
                SingleDeviceListAdapter adapter = new SingleDeviceListAdapter(context, R.layout.single_device_adapter_view, mBTDevices);
            listSearched.setAdapter(adapter);
            } else {
                Toast.makeText(getApplicationContext(), R.string.no_paried_devices_bluetooth, Toast.LENGTH_LONG).show();
            }
            listSearched.setOnItemClickListener(myListClickListener);
        }
    };
    private  BroadcastReceiver bondChangedReceived = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            if(action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)){
                Log.d(TAG, "Changed bond state");
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                if (device.getBondState() == BluetoothDevice.BOND_BONDING) {
                    Log.d(TAG, "if BOND_BONDING");
                }
                if (device.getBondState() == BluetoothDevice.BOND_NONE) {
                    Log.d(TAG, "if BOND_NONE");
                }
                if (device.getBondState() == BluetoothDevice.ERROR) {
                    Log.d(TAG, "if ERROR");
                }
                if (device.getBondState() == BluetoothDevice.BOND_BONDED) {
                    Log.d(TAG, "if BOND");
                }
            }
        }
    };
    private BroadcastReceiver discoveryFinishedReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            if(action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)){
                Log.d(TAG, "Discovery finished");
                Toast.makeText(getApplicationContext(), R.string.discovery_finished, Toast.LENGTH_LONG).show();
            }
        }
    };
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (myBluetooth != null) {
            myBluetooth.cancelDiscovery();
        }
        unregisterReceiver(discoveryDeviceReceiver);
        unregisterReceiver(bondChangedReceived);
        unregisterReceiver(discoveryFinishedReceiver);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnSearch = (Button) findViewById(R.id.buttonSearch);
        btnParied = (Button) findViewById(R.id.buttonParied);
        listPaired = (ListView) findViewById(R.id.listPairedDevices);
        listSearched = (ListView) findViewById(R.id.listSearchedDevices);
        myBluetooth = BluetoothAdapter.getDefaultAdapter();
        if (myBluetooth == null) {
            Toast.makeText(getApplicationContext(), R.string.uncapabilities_bluetooth, Toast.LENGTH_LONG).show();
            finish();
        } else if (!myBluetooth.isEnabled()) {
            Intent turnBTon = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(turnBTon, 1);
        }
    }
    public void btnClickSearch(View view) {
         mBTDevices = null;
        if (myBluetooth.isDiscovering()) {
            myBluetooth.cancelDiscovery();
            unregisterReceiver(discoveryDeviceReceiver);
            unregisterReceiver(bondChangedReceived);
            unregisterReceiver(discoveryFinishedReceiver);
            checkBTPermissions();
            myBluetooth.startDiscovery();
            IntentFilter discoverDevicesIntent = new IntentFilter(BluetoothDevice.ACTION_FOUND);
            IntentFilter bondChangedIntent = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
            IntentFilter discoverFinishedDevicesIntent = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
            registerReceiver(discoveryDeviceReceiver, discoverDevicesIntent);
            registerReceiver(bondChangedReceived, bondChangedIntent);
            registerReceiver(discoveryFinishedReceiver, discoverFinishedDevicesIntent);
        }
        else {
            checkBTPermissions();
            myBluetooth.startDiscovery();
            IntentFilter discoverDevicesIntent = new IntentFilter(BluetoothDevice.ACTION_FOUND);
            IntentFilter bondChangedIntent = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
            IntentFilter discoverFinishedDevicesIntent = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
            registerReceiver(discoveryDeviceReceiver, discoverDevicesIntent);
            registerReceiver(bondChangedReceived, bondChangedIntent);
            registerReceiver(discoveryFinishedReceiver, discoverFinishedDevicesIntent);
        }
    }
    public void btnClickParied(View view) {
        pairedDevicesList();
    }
    private void pairedDevicesList() {
        Set<BluetoothDevice> setPairedDevices = myBluetooth.getBondedDevices();
        pairedDevices = new ArrayList<BluetoothDevice>();
        if (setPairedDevices.size() > 0) {
            for (BluetoothDevice bt : setPairedDevices) {
                Log.d(TAG, bt.getAddress());
                pairedDevices.add(bt);
            }
            final SingleDeviceListAdapter adapter = new SingleDeviceListAdapter(MainActivity.this, R.layout.single_device_adapter_view, pairedDevices);
            listPaired.setAdapter(adapter);
            listPaired.setOnItemClickListener(myPairedListClickListener);
        } else {
            Toast.makeText(getApplicationContext(), R.string.no_found_paried_devices, Toast.LENGTH_LONG).show();
        }
    }

和AdapterView的监听器

    private AdapterView.OnItemClickListener myPairedListClickListener = new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> av, View v, int arg2, long arg3) {
            myBluetooth.cancelDiscovery();
            String address = pairedDevices.get(arg2).getAddress();
            Intent i = new Intent(MainActivity.this, LedControl.class);
            i.putExtra(EXTRA_ADDRESS, address);
            startActivity(i);
        }
    };
    private AdapterView.OnItemClickListener myListClickListener = new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> av, View v, int arg2, long arg3) {
            myBluetooth.cancelDiscovery();
            String deviceAddress = mBTDevices.get(arg2).getAddress();
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR2) {
                Log.d(TAG, "before createBond()");
                mBTDevices.get(arg2).createBond();
                Log.d(TAG, "after createBond()");
                Intent i = new Intent(MainActivity.this, LedControl.class);
                i.putExtra(EXTRA_ADDRESS, deviceAddress);
                startActivity(i);
            }
        }
    };
    private void checkBTPermissions() {
        ...
            }
        } 
    }
}

如果您在代码中看到错误,请指出我。感谢名单!

0 个答案:

没有答案