Пример #1
0
 @Override
 public void onActivityResult(int requestCode, int resultCode, Intent data) {
   switch (requestCode) {
     case REQUEST_DEVICES:
       // When DeviceListActivity returns with a device to connect
       devicesShowing = false;
       if (resultCode == Activity.RESULT_OK) {
         // Get the device MAC address
         String address = data.getExtras().getString(Devices.EXTRA_DEVICE_ADDRESS);
         // Get the BLuetoothDevice object
         BluetoothDevice device = bluetoothAdapter.getRemoteDevice(address);
         // Attempt to connect to the device
         communicator.connect(device);
       }
       break;
     case REQUEST_ENABLE_BT:
       // When the request to enable Bluetooth returns
       if (resultCode == Activity.RESULT_OK) {
         // Bluetooth is now enabled
       } else {
         // User did not enable Bluetooth or an error occured
         Toast.makeText(this, R.string.bluetooth_disabled, Toast.LENGTH_LONG).show();
         finish();
       }
   }
 }
Пример #2
0
 protected void connect() {
   Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
   if (pairedDevices.size() == 1) {
     communicator.connect(pairedDevices.iterator().next());
   } else {
     Intent serverIntent = new Intent(this, Devices.class);
     startActivityForResult(serverIntent, REQUEST_DEVICES);
     devicesShowing = true;
   }
 }