Using a Bluetooth device for Bluetooth devices

My application requires a Bluetooth connection. And at the first stage, I try to open the standard Activity "Select a Bluetooth device" to help the user search for a new device or select a device from the list.

The problem is that I cannot get any working example for the device of a Bluetooth device. The task is simple. To trigger an action with the intent of "android.bluetooth.devicepicker.action.LAUNCH"

And the device selection device opens without problems.

But four additions are required to select a device, and I cannot determine the exact parameters for the two additional settings listed below.

.putExtra("android.bluetooth.devicepicker.extra.LAUNCH_PACKAGE","com.extreme.controlcenter" .putExtra("android.bluetooth.devicepicker.extra.DEVICE_PICKER_LAUNCH_CLASS","com.extreme.controlcenter.WelcomeActivity") 

I thought the parameters should be

* "android.bluetooth.devicepicker.extra.LAUNCH_PACKAGE" *

should have the name of my package, so I passed this only. This is "com.extreme.controlcenter"

The second should be the name of the component that should receive the broadcast that runs after the device is selected. Here I tried to put the name of a class that has an onReceive () function.

But the problem is that the onReceive () function is NOT called when the device is selected in the device selection device!

 public void onReceive(Context context, Intent intent) { String action = intent.getAction(); //Device Selected on Device Picker if("android.bluetooth.devicepicker.action.DEVICE_SELECTED".equals(action)) { //context.unregisterReceiver(this); BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); Toast.makeText(context, "device" + device.getAddress(), Toast.LENGTH_SHORT).show(); String MAC = device.getAddress(); //Log.d("my", MAC); Intent intent2 = new Intent(WelcomeActivity.this, ControlActivity.class); intent2.putExtra(EXTRA_DEVICE_ADDRESS, MAC); startActivity(intent2); } }; 

I created an Intent filter and registered receipt in main action onCreate ()

  // Register the BroadcastReceiver IntentFilter filter = new IntentFilter("android.bluetooth.devicepicker.action.DEVICE_SELECTED"); registerReceiver(mReceiver, filter); 

It is one thing that if I did not provide these two add-ons, the Broadcast event will be successfully received. But this code only works on my TAB, but the same crash on my cell phone. Therefore, I think that the provision of these two additional services is mandatory.

Thank Advance

+4
source share
1 answer

"com.extreme.controlcenter.WelcomeActivity" in your EXTRA should be a BroadcastReceiver class, such as MyBroadcastReceiver.class.getName (). I also declared it in the manifest inside the tags

+1
source

Source: https://habr.com/ru/post/1439804/


All Articles