I've implemented a list with all of my paired devices, and now I'd like to know if it's possible to connect to some of them only with clicking on the item.
For example if my list contains a bluetooth device called X and I want to connect to it (with my app) click on it and the connection is stablished between device and my phone.
This is how I list my paired devices :
myListView = (ListView) dialog.findViewById(R.id.BTList);
BTArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
myListView.setAdapter(BTArrayAdapter);
pairedDevices = myBluetoothAdapter.getBondedDevices();
for(BluetoothDevice device : pairedDevices)
BTArrayAdapter.add(device.getName()+ "\n" + device.getAddress());
If you know the name of the device you wish to pair to you can use an equals comparison.
private static final String DEVICE_WE_WANT_TO MATCH = "X";
String devName = device.getName();
if(devName.equals(DEVICE_WE_WANT_TO MATCH)){
// Connect.
}
You can also use an app UUID
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
This will mean that only devices using this UUID will connect using your protocol, it's an extra layer of security for the app.
In this, latter, case, we're relying on one android device to be acting as a BT server and the other as a BT client.
Related
I'm currently making an app in Android Studio that should communicate with the HC06 Bluetooth Module.
I've started with a code I found online here :
http://android-er.blogspot.com/2015/10/android-communicate-with-arduino-hc-06.html
It works but I'm trying to understand the following part (the comments are mine so it may be wrong)
private void setup() {
//store the bluetooth bonded devices in Bluetooth Devices List
Set<BluetoothDevice> PairedDevices = BluetoothAdapter.getBondedDevices();
// check if we have something in the list
if (PairedDevices.size() > 0) {
PairedDeviceArrayList = new ArrayList<BluetoothDevice>();
//add each Bluetooth Device in an array
for (BluetoothDevice device : PairedDevices) {
PairedDeviceArrayList.add(device);//put everything in a
}
PairedDeviceAdapter = new ArrayAdapter<BluetoothDevice>(this, android.R.layout.simple_list_item_1, PairedDeviceArrayList);
ListViewPairedDevice.setAdapter(PairedDeviceAdapter);
}
}
Why is the "PairedDeviceList" needed? I had the PairedDevices list that holds the Bluetooth devices.
Also, I'm not sure how an "adapter" object works, so if u could help me with some explanation it would mean a lot.
If you could find an easier way to list the devices feel free to share, please.
Thanks
In my Android app I want to detect all Android device names found in the local wireless network. I am able to scan the network and find the devices IP and full qualified domain name (FQDN) like android-2120ee3b45******. I'm doing it like:
final InetAddress inetAddress = InetAddress.getByName(ip);
if (inetAddress.isReachable(400)) {
final String host = inetAddress.getHostName();
final String canHost = inetAddress.getCanonicalHostName();
final String ip = inetAddress.getAddress();
}
With java.net.InetAddress I only get the IP and the network name like android-2120ee3b45******. But I want the Android device name defined by the user on the device like "Peters Fire TV" or "Mikes Samsung SGS6". I saw apps like AllConnect or AllCast which can grab such name from Fire TV (which is a android device).
How can I get the Android device name defined by the user over the WIFI network?
add this line,
for(i=0;i<WifiP2pDeviceList.size();i++){
WifiP2pDevice device = WifiP2pDeviceList.get(i);
String deviceName=device.deviceName;
String devicestatus=device.status;
//so on
}
I'm trying to detect if a bonded Bluetooth Device supports GATT or not.
When scanning, calling BluetoothDevice.getType() will recognized my device as type 3 (Dual Mode - BR/EDR/LE). However, after the device is bonded and a call to BluetoothAdapter.getBondedDevices(), the same method returns my device as type 1 (Classic).
#Override
public void onScanResult(int callbackType, android.bluetooth.le.ScanResult result) {
result.getDevice().getType();// value is 3
}
...
// this will show pairing request to user
device.connectGatt(context, false, callback);
...
// once the device is paired, I query for the new set of bonded devices.
Set<BluetoothDevice> set = BluetoothAdapter.getDefaultAdapter().getBondedDevices();
for (BluetoothDevice device : set)
{
device.getType();// value is 1
}
How can I reliably detect if a bonded device support GATT (type 3 or 2)?
I have also tried cross-checking bonded devices with:
int[] ALL_STATES = { BluetoohtProfile.STATE_DISCONNECTED, BluetoohtProfile.STATE_CONNECTING, BluetoohtProfile.STATE_CONNECTED, BluetoohtProfile.STATE_DISCONNECTING };
List<BluetoothDevice> list = BluetoothManager.getDevicesMatchingConnectionState(BluetoothProfile.GATT, ALL_STATES);
But the result is always an empty list.
Appreciate any help!
You can double check whether the device in your scan result and bonded device is the same device.
I guess the bonded device is other BT device bonded with your Android device.
I am trying to control a Hands-Free link with my device. The following works just fine:
UUID HFP_UUID_GET_HF = UUID.fromString("0000111E-0000-1000-8000-00805F9B34FB");
BluetoothSocket aBluetoothSocket = mDevice
.createRfcommSocketToServiceRecord(HFP_UUID_GET_HF);
and I get a socket that I can read and right to. No problem. However, I also want to listen for an incoming connection and get that socket. I tried this:
UUID HFP_UUID = UUID.fromString("0000111F-0000-1000-8000-00805F9B34FB");
UUID HFP_UUID_GET_HF = UUID.fromString("0000111E-0000-1000-8000-00805F9B34FB");
BluetoothServerSocket tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord("HFP", HFP_UUID);
BluetoothSocket aBluetoothSocket = tmp.accept();
However, even though the two devices connect I never get a socket back. BTW if I use the UUID that starts with 111E in this second code block here I get a service discovery io error, which makes sense -- I know that my device is using uuid 111F and the other device uses UUID 111E.
Has anyone ran into this issue before? I need to be able to have complete control over all data that gets sent from the phone on that rfcomm channel. I cannot use reflection ; i.e.
Class<?>[] args = new Class[] { int.class };
int HFP_CHANNEL = 10;
Method listenOn = BluetoothAdapter.class.getDeclaredMethod("listenUsingRfcommOn", args);
BluetoothServerSocket my_server = (BluetoothServerSocket) (listenOn.invoke(mBluetoothAdapter,
new Object[] { HFP_CHANNEL }));
BluetoothSocket m_BluetoothSocket = my_server.accept();
because that also throws an io error -- channel already in use, unless anyone knows a way to turn off the hands-free system service. Afaik that is part of bluetoothd (Im using Android 4.1 here) and I need that to remain running (Im not sure if I even can turn it off)
I am trying to get the list of the bonded bluetooth devices on the phone.
My problem is that I am getting it in a "non friendly" way.
Here is my code:
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
ArrayList<String> listview =
new ArrayList<String>(Arrays.asList(pairedDevices.toString()));
I'm getting: 00:23:7f:5f:fe:1c...
How can i get the friendly names and not numbers?
Just to add that I know about getname(), but as I understand its only for a connected device and not for the bonded devices.
the getName() method of the BluetoothDevice device class will help you out. Just iterate through the Set and call the getName() method on each BluetoothDevice object.