0

Normally how Android Broadcast work is: app have to create BroadcastReceiver and have to register action intent for it want to get receive event.

But in case of Bluetooth device discovery/scanning why it required request call through BluetoothAdapter.startDsiccovery().

Basically I want to dicover BLE device through long live Service running in Background.

Any one have idea here?

CoDe
  • 11,056
  • 14
  • 90
  • 197

4 Answers4

1
private void listenPairedDevice() {
Button listenBtn = (Button)findViewById(R.id.button_listen);
listenBtn.setOnClickListener(new OnClickListener() {
  public void onClick(View view) {
    Intent disc = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
    startActivityForResult(disc, DISCOVERY_REQUEST);     
 }
 });
}
@Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 if (requestCode == DISCOVERY_REQUEST) {
boolean isDiscoverable = resultCode > 0;
 if (isDiscoverable) {
 String name = "bluetoothserver";
 try {
final BluetoothServerSocket btserver = bluetooth.listenUsingRfcommWithServiceRecord(name, uuid);


      AsyncTask<Integer, Void, BluetoothSocket> acceptThread = new AsyncTask<Integer, Void, BluetoothSocket>() {

        @Override
        protected BluetoothSocket doInBackground(Integer... params) {
          try {

             socket = btserver.accept();
            return socket;
             } catch (IOException e) {
            Log.d("BLUETOOTH", e.getMessage());            
          }
          finally {
            //close statement added later by MR
              try{
              btserver.close();
              } catch (IOException e){

              }
          }
          return null;
        }

        @Override
        protected void onPostExecute(BluetoothSocket result) {
          if (result != null)
            changeLayout();
        }            
      };          
      acceptThread.execute(resultCode);
    } catch (IOException e) {
      Log.d("BLUETOOTH", e.getMessage());            
    }
  }
}
learner
  • 3,092
  • 2
  • 21
  • 33
  • using this code you can able to discover **bluetooth** devices, – learner Jan 06 '14 at 07:34
  • discovering device was not at all issue here...what I asked for is there any way to run scanning continuous in BG service...ususally for same we have "startDiscovery()" call...but every time app have to request to get broadcast...any other idea? – CoDe Jan 06 '14 at 07:53
0

The startDiscovery() does a 2 step process,

  • Finding the device by inquiring for it.

  • Followed by a name discovery ie paging and connecting to the device.

If you have gone through the docs it says public boolean startDiscovery ()

Start the remote device discovery process.

The discovery process usually involves an inquiry scan of about 12 seconds, followed by a page scan of each new device to retrieve its Bluetooth name.

This is an asynchronous call, it will return immediately. Register for ACTION_DISCOVERY_STARTED and ACTION_DISCOVERY_FINISHED intents to determine exactly when the discovery starts and completes. Register for ACTION_FOUND to be notified as remote Bluetooth devices are found.

Device discovery is a heavyweight procedure. New connections to remote Bluetooth devices should not be attempted while discovery is in progress, and existing connections will experience limited bandwidth and high latency. Use cancelDiscovery() to cancel an ongoing discovery. Discovery is not managed by the Activity, but is run as a system service, so an application should always call cancelDiscovery() even if it did not directly request a discovery, just to be sure.

Device discovery will only find remote devices that are currently discoverable (inquiry scan enabled). Many Bluetooth devices are not discoverable by default, and need to be entered into a special mode.

If Bluetooth state is not STATE_ON, this API will return false. After turning on Bluetooth, wait for ACTION_STATE_CHANGED with STATE_ON to get the updated value.

Requires BLUETOOTH_ADMIN.

Returns true on success, false on error

EDIT

Plese follow Bluetooth device discovery in Android — startDiscovery()

Community
  • 1
  • 1
  • Thanks and appreciate for your answer...it's true device scanning in heavy process...but in android doc they never mention anywhere about to do "scan request" to get Broadcast(BC)..normal behaviour of BC is...app need to register BC with required Action...that's it...n system will work for ur app...please mention here if u got any related post – CoDe Jan 06 '14 at 06:10
  • http://stackoverflow.com/questions/10560319/bluetooth-device-discovery-in-android-startdiscovery please go through this link. –  Jan 06 '14 at 06:17
  • not helped dear...any idea to run continuous scan process in background...or specific to as device? – CoDe Jan 06 '14 at 07:27
0

I think you have to start on Bluetooth discovery using UUId and socket.

After getting any device you have to set up a connection to this and start a thread that will check regularly while it is connected or not.

AnAndroid
  • 567
  • 1
  • 5
  • 16
  • same I do not want..not want to do thread/timer based request from app ...basically if System Broadcast service help...so at app level no worry to maintain battery drainage. – CoDe Jan 06 '14 at 08:13
  • i don't think it will work without thread because you have to check whether it is connected or not, may be possible that connection will fail after sometime. Using broadcast you can only check that Bluetooth device is available or not. – AnAndroid Jan 06 '14 at 08:20
0

Scanning for peripherals is not just a heavy-weight task, it is also not friendly to the battery so it can't be left on all the time.

That said, you can still have something in the background doing the work for you, like something that wakes up every 15 seconds and scans for 5 seconds. On some devices when you try to connect to a device that is no longer available you will get a disconnected callback to onConnectionStateChange in the BluetoothGattCallback, and on other devices the same connection attempt will wait until the device is back and then connect.

So basically, wait, scan, connect if you find your device and if not then wait again.

Douglas Jones
  • 2,522
  • 1
  • 23
  • 27