To connect a Bluetooth device with embedded device,I want user's click on discovered device and auto pair with 0000 pin (No need for confirmation and also important 0000 passkey), not with creating bond with passkeys,I don't have any connection related issue
I found following code I am not sure its working or not also don't know on how to pair Device(phone) to another device(Embedded device) with pin="0000" in this code??
Here is some part of code for connectivity.
OnItemClicklistner on discoverd(scanned ) devices
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
IntentFilter filter = new IntentFilter(
"android.bluetooth.device.action.PAIRING_REQUEST");
registerReceiver(mPairingRequestReceiver, filter);
}
This is my pairing Receiver for connection with 0000 pin
private final BroadcastReceiver mPairingRequestReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(BluetoothDevice.ACTION_PAIRING_REQUEST)) {
try {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// int pin=intent.getIntExtra("android.bluetooth.device.extra.PAIRING_KEY", 0);
//the pin in case you need to accept for an specific pin
String pin="0000";
Log.d(TAG, "Start Auto Pairing. PIN = " + intent.getIntExtra("android.bluetooth.device.extra.PAIRING_KEY",0));
byte[] pinBytes;
// pinBytes = (""+pin).getBytes("UTF-8");
pinBytes = pin.getBytes();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
device.setPin(pinBytes);
device.setPairingConfirmation(true);
}
} catch (Exception e) {
Log.e(TAG, "Error occurs when trying to auto pair");
e.printStackTrace();
}
}
}
};
Note : Creat bond is working here. I dont want to creat bond just on click Item and connect to embedded device with 0000 pin.... Thank you..
Related
I have managed to make an android device and scan and enlist all bluetooth devices in the area..I have the mac address of a remote device i just discovered in form of string and i want to start the bonding process with it.. i try BluetoothDevice object and then method createBond() but its not communicating with the remote device to pair..
Here is the code
class BluetoothM: AppCompatActivity{
// mac address of remote bluetooth device
string address;
//the discovered devices are listed in a ListView so i call a listview item click method to start pairing
private void Dlist_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
//the mac address has already been assigned in the OnReceive broadcast Receiver
if (e.Position == e.Id)
{
//Get the default adapter of the device
BluetoothAdapter adapt = BluetoothAdapter.DefaultAdapter;
//Get the remote device based on its MAC address
BluetoothDevice device = adapt.GetRemoteDevice(address);
//start the pairing process for the device
device.CreateBond();
}
}
}
//This class discovers bluetooth devices and pass MAC address to our string address for use in the listview click method
[BroadcastReceiver(Name = BluetoothDevice.ActionFound, Enabled = true)]
public class DeviceDiscovered : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
string ac = intent.Action; string name;
if (BluetoothDevice.ActionFound.Equals(ac))
{
BluetoothDevice device = (BluetoothDevice)intent.GetParcelableExtra(BluetoothDevice.ExtraDevice);
//add bluetooth name and address if they do not already exist
if (!MainActivity.avail.Contains(device.Name + "\n" + device.Address))
{
MainActivity.avail.Add(device.Name + "\n" + device.Address);
ArrayAdapter arrayAdapter1 = new ArrayAdapter(context, Android.Resource.Layout.SimpleListItem1, MainActivity.avail);
MainActivity.dlist.Adapter = arrayAdapter1;
//address assigned a value
MainActivity.address = device.Address; MainActivity.name = device.Name;
MainActivity.mydevice = device;
}
}
Toast.MakeText(context, "Received the intent", ToastLength.Short).Show();
}
The remote device is not receiving this communication and i don't know why, Thank You
Check you have BLUETOOTH_ADMIN permission in Manifest. Register for ACTION_BOND_STATE_CHANGED intents to be notified when the bonding process completes, and its result. Get the result of device.CreateBond(), it returns false on immediate error, true if bonding will begin.
https://developer.android.com/reference/android/bluetooth/BluetoothDevice#createBond()
Current I am working on OBDII. While in bluetooth discovery to connect obd, How I determine which device is obd and which device is normal bluetooth device.Because I want auto connect my app with obd.
Is there any common characteristic of obd device which help me to determine this is OBD device?
My Code which I am trying for pairing obd device as per name.
//Register receiver for bluetooth discovery
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
//discovery starts, we can show progress dialog or perform other tasks
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
//discovery finishes, dismiss progress dialog
} else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
//bluetooth device found for pair
try {
// Here I want to know device is obd or not?
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//Make pair of obd device as per name:
if (device != null && !device.getName().equals("")) {
LogUtils.LOGE("NEW DEVICE", device.getName());
if (device.getName().equals(OBD_DEVICE_NAME_ONE) ||
device.getName().equals(OBD_DEVICE_NAME_TWO) ||
device.getName().equals(OBD_DEVICE_NAME_THREE)) {
pairDevice(device);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
//Send pairing request to OBD Device
private void pairDevice(BluetoothDevice device) {
try {
Method method = device.getClass().getMethod("createBond", (Class[]) null);
method.invoke(device, (Object[]) null);
} catch (Exception e) {
e.printStackTrace();
}
}
After Pairing OBD Device with name, It will autoconnect and work fine.
Only good way to find out, is to connect and to send:
ATI
Which will return a ID, which always contain ELM327.
Using Google Glass, I am able to discover Bluetooth devices and see their address and information. However, I cannot get the Glass to pair (bond) with them.
Update
Following the instructions on this page now I'm trying to get the bonding, but for some reason the BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action) is never happening.
private void pairDevice(BluetoothDevice Ddevice) {
Log.d("MY_LOG", "Try to pair " + Ddevice.getName());
try{
Method m = Ddevice.getClass().getMethod("createBond", (Class[]) null);
m.invoke(Ddevice, (Object[]) null);
Log.d("MY_LOG", "Pairing " + Ddevice.getName());
}catch(Exception e){
Log.d("MY_LOG", "Error: ");
e.printStackTrace();
}
}
In the LOG I always get "Pairing DeviceName" but when I search for the bonded devices, it remains empty.
Any help will be greatly appreciated.
So I will answer my own question as I just found the way.
So first, the discovery of devices is quite easy, in onCreate() I used (besides all other sort of code you need):
MyBT = BluetoothAdapter.getDefaultAdapter();
MyBT.startDiscovery();
Filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); // Register the BroadcastReceiver
Filter2 = new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST); // Register the Bond changing state
registerReceiver(mReceiver, Filter); // Don't forget to unregister during onDestroy
registerReceiver(mReceiver, Filter2); // ******
Then at the BroadcastReceiver you need to manage the devices and the pairing requests:
private final BroadcastReceiver mReceiver = new BroadcastReceiver() { // Create a BroadcastReceiver for ACTION_FOUND
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) { // When discovery finds a device
BluetoothDevice BTdevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); // Get the BluetoothDevice object from the Intent
ListDev.add(BTdevice); // Add the device to an array adapter to show...
}
if(BluetoothDevice.ACTION_PAIRING_REQUEST.equals(action)){
BluetoothDevice device = ListDev.get(selectedDevice);
byte[] pinBytes = getStrFromName(device.getName(),7,11).getBytes(); // My devices had their own pin in their name, you can put a constant pin here...
try {
Log.d("MY_LOG", "Try to set the PIN");
Method m = device.getClass().getMethod("setPin", byte[].class);
m.invoke(device, pinBytes);
Log.d("MY_LOG", "Success to add the PIN.");
try {
device.getClass().getMethod("setPairingConfirmation", boolean.class).invoke(device, true);
Log.d("MY_LOG", "Success to setPairingConfirmation.");
} catch (Exception e) {
Log.e("MY_LOG", e.getMessage());
e.printStackTrace();
}
} catch (Exception e) {
Log.e("MY_LOG", e.getMessage());
e.printStackTrace();
}
}
}
};
After that the device is bonded and you can manage the connection with the UUID and the sockets just as in the Android webpage example.
Hope this helps!
In my Android app, I read the values from a 3DConnexion SpaceNavigator via USB-OTG to control an AR.Drone.
Now I want to do the same with a mouse. However, Android is grabbing the mouse and presenting a mouse-cursor. When I write a device-filter with the vendor and product ID of the mouse, I do not get it like with the SpaceNavigator (strangely, both are HID -- I get no cursor with the SpaceNavigator).
Is there a way to get the raw mouse data without the cursor?
Would be perfect with stock Android. but I would also consider altering the ROM for that.
As soon as your Application claims the Mouse (as a USB HID device while being Host), Android should hide the cursor and you can read the raw data. This should work on stock android, but your device has to support USB Host mode and a USB OTG cable will be needed to connect the mouse.
Basic procedure:
enumerate devices
ask for permission to access the USB device
claim the device
read a data package from the HID endpoint
parse the X and Y position, button clicks and scroll wheel rotation from the data package
Example Code that works for me (Android 5.0):
UsbManager usbManager;
UsbDevice usbDevice;
private void connect() {
this.usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();
// just get the first enumerated USB device
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
if (deviceIterator.hasNext()) {
this.usbDevice = deviceIterator.next();
}
if (usbDevice == null) {
Log.w(TAG, "no USB device found");
return;
}
// ask for permission
final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION_USB_PERMISSION.equals(action)) {
synchronized (this) {
UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
if(device != null){
// call method to set up device communication
Log.i(TAG, "permission granted. access mouse.");
// repeat in a different thread
transfer(device);
}
}
else {
Log.d(TAG, "permission denied for device " + device);
}
}
} else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (device != null) {
// TODO:
// call your method that cleans up and closes communication with the device
// usbInterface.releaseInterface();
// usbDeviceConnection.close();
}
}
}
};
PendingIntent mPermissionIntent = PendingIntent.getBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
context.registerReceiver(mUsbReceiver, filter);
usbManager.requestPermission(usbDevice, mPermissionIntent);
}
private void transfer(UsbDevice device) {
int TIMEOUT = 0;
boolean forceClaim = true;
// just grab the first endpoint
UsbInterface intf = device.getInterface(0);
UsbEndpoint endpoint = intf.getEndpoint(0);
UsbDeviceConnection connection = this.usbManager.openDevice(device);
connection.claimInterface(intf, forceClaim);
byte[] bytes = new byte[endpoint.getMaxPacketSize()];
connection.bulkTransfer(endpoint, bytes, bytes.length, TIMEOUT);
// depending on mouse firmware and vendor the information you're looking for may
// be in a different order or position. For some logitech devices the following
// is true:
int x = (int) bytes[1];
int y = (int) bytes[2];
int scrollwheel = (int) bytes[3]
// call a listener, process your data ...
}
How can I list all connected bluetooth devices on android ?
thanks!
public void checkConnected()
{
// true == headset connected && connected headset is support hands free
int state = BluetoothAdapter.getDefaultAdapter().getProfileConnectionState(BluetoothProfile.HEADSET);
if (state != BluetoothProfile.STATE_CONNECTED)
return;
try
{
BluetoothAdapter.getDefaultAdapter().getProfileProxy(_context, serviceListener, BluetoothProfile.HEADSET);
}
catch (Exception e)
{
e.printStackTrace();
}
}
private ServiceListener serviceListener = new ServiceListener()
{
#Override
public void onServiceDisconnected(int profile)
{
}
#Override
public void onServiceConnected(int profile, BluetoothProfile proxy)
{
for (BluetoothDevice device : proxy.getConnectedDevices())
{
Log.i("onServiceConnected", "|" + device.getName() + " | " + device.getAddress() + " | " + proxy.getConnectionState(device) + "(connected = "
+ BluetoothProfile.STATE_CONNECTED + ")");
}
BluetoothAdapter.getDefaultAdapter().closeProfileProxy(profile, proxy);
}
};
As of API 14 (Ice Cream), Android has a some new BluetoothAdapter methods including:
public int getProfileConnectionState (int profile)
where profile is one of HEALTH, HEADSET, A2DP
Check response, if it's not STATE_DISCONNECTED you know you have a live connection.
Here is code example that will work on any API device:
BluetoothAdapter mAdapter;
/**
* Check if a headset type device is currently connected.
*
* Always returns false prior to API 14
*
* #return true if connected
*/
public boolean isVoiceConnected() {
boolean retval = false;
try {
Method method = mAdapter.getClass().getMethod("getProfileConnectionState", int.class);
// retval = mAdapter.getProfileConnectionState(android.bluetooth.BluetoothProfile.HEADSET) != android.bluetooth.BluetoothProfile.STATE_DISCONNECTED;
retval = (Integer)method.invoke(mAdapter, 1) != 0;
} catch (Exception exc) {
// nothing to do
}
return retval;
}
First you need to retrieve the BluetoothAdapter:
final BluetoothAdapter btAdapter =
BluetoothAdapter.getDefaultAdapter();
Second you need to make sure Bluetooth is available and turned on :
if (btAdapter != null && btAdapter.isEnabled()) // null means no
Bluetooth!
If the Bluetooth is not turned out you can either use btAdapter.enable() which is not recommended in the documentation or ask the user to do it : Programmatically enabling bluetooth on Android
Third you need to define an array of states (to filter out
unconnected devices):
final int[] states = new int[] {BluetoothProfile.STATE_CONNECTED,
BluetoothProfile.STATE_CONNECTING};
Fourth, you create a BluetoothProfile.ServiceListener which
contains two callbacks triggered when a service is connected and
disconnected :
final BluetoothProfile.ServiceListener listener = new BluetoothProfile.ServiceListener() {
#Override
public void onServiceConnected(int profile, BluetoothProfile proxy) {
}
#Override
public void onServiceDisconnected(int profile) {
}
};
Now since you have to repeat the querying process for all available Bluetooth Profiles in the Android SDK (A2Dp, GATT, GATT_SERVER, Handset, Health, SAP) you should proceed as follow :
In onServiceConnected, place a condition that check what is the current profile so that we add the found devices into the correct collection and we use : proxy.getDevicesMatchingConnectionStates(states) to filter out unconnected devices:
switch (profile) {
case BluetoothProfile.A2DP:
ad2dpDevices.addAll(proxy.getDevicesMatchingConnectionStates(states));
break;
case BluetoothProfile.GATT: // NOTE ! Requires SDK 18 !
gattDevices.addAll(proxy.getDevicesMatchingConnectionStates(states));
break;
case BluetoothProfile.GATT_SERVER: // NOTE ! Requires SDK 18 !
gattServerDevices.addAll(proxy.getDevicesMatchingConnectionStates(states));
break;
case BluetoothProfile.HEADSET:
headsetDevices.addAll(proxy.getDevicesMatchingConnectionStates(states));
break;
case BluetoothProfile.HEALTH: // NOTE ! Requires SDK 14 !
healthDevices.addAll(proxy.getDevicesMatchingConnectionStates(states));
break;
case BluetoothProfile.SAP: // NOTE ! Requires SDK 23 !
sapDevices.addAll(proxy.getDevicesMatchingConnectionStates(states));
break;
}
And finally, the last thing to do is start the querying process :
btAdapter.getProfileProxy(yourContext, listener, BluetoothProfile.A2DP);
btAdapter.getProfileProxy(yourContext, listener, BluetoothProfile.GATT); // NOTE ! Requires SDK 18 !
btAdapter.getProfileProxy(yourContext, listener, BluetoothProfile.GATT_SERVER); // NOTE ! Requires SDK 18 !
btAdapter.getProfileProxy(yourContext, listener, BluetoothProfile.HEADSET);
btAdapter.getProfileProxy(yourContext, listener, BluetoothProfile.HEALTH); // NOTE ! Requires SDK 14 !
btAdapter.getProfileProxy(yourContext, listener, BluetoothProfile.SAP); // NOTE ! Requires SDK 23 !
source: https://stackoverflow.com/a/34790442/2715054
So you get the list of paired devices.
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDevicesList = btAdapter.getBondedDevices();
for (BluetoothDevice pairedDevice : pairedDevicesList) {
Log.d("BT", "pairedDevice.getName(): " + pairedDevice.getName());
Log.d("BT", "pairedDevice.getAddress(): " + pairedDevice.getAddress());
saveValuePreference(getApplicationContext(), pairedDevice.getName(), pairedDevice.getAddress());
}
Android system doesn't let you query for all "currently" connected devices. It however, you can query for paired devices. You will need to use a broadcast receiver to listen to ACTION_ACL_{CONNECTED|DISCONNECTED} events along with STATE_BONDED event to update your application states to track what's currently connected.
I found a solution and it works on android 10
Kotlin
private val serviceListener: ServiceListener = object : ServiceListener {
var name: String? = null
var address: String? = null
var threadName: String? = null
override fun onServiceDisconnected(profile: Int) {}
override fun onServiceConnected(profile: Int, proxy: BluetoothProfile) {
for (device in proxy.connectedDevices) {
name = device.name
address = device.address
threadName = Thread.currentThread().name
Toast.makeText(
this#MainActivity,
"$name $address$threadName",
Toast.LENGTH_SHORT
).show()
Log.i(
"onServiceConnected",
"|" + device.name + " | " + device.address + " | " + proxy.getConnectionState(
device
) + "(connected = "
+ BluetoothProfile.STATE_CONNECTED + ")"
)
}
BluetoothAdapter.getDefaultAdapter().closeProfileProxy(profile, proxy)
}
}
Call this method in main thread
BluetoothAdapter.getDefaultAdapter()
.getProfileProxy(this, serviceListener, BluetoothProfile.HEADSET)
Java
original code
Please analyze this class online.
Here you will find how to discover all connected (paired) Bluetooth devices.
Well here are the steps:
First, you start intent to discover devices
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
Register a broadcast reciver for it:
registerReceiver(mReceiver, filter);
On the definition of mReceiver:
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Add the name and address to an array adapter to show in a ListView
arrayadapter.add(device.getName())//arrayadapter is of type ArrayAdapter<String>
lv.setAdapter(arrayadapter); //lv is the list view
arrayadapter.notifyDataSetChanged();
}
}
and the list will be automatically populated on new device discovery.