I am running into an issue where my Arduino 101 device will not be discovered by a BLE scan. However, if I scan for normal bluetooth devices it shows up.
I am attempting to use the BLE Gatt example by Google here https://developer.android.com/samples/BluetoothLeGatt/project.html but the device does not show. Even with the unmodified code.
However if I query for connected bluetooth devices it shows my device.
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
String name = "";
for (BluetoothDevice device : pairedDevices) {
if (device.getName().equals("B & S")) {
bsDevice = device;
name += device.getName();
}
}
If I attempt to connect to the device using createRfcommSocketToServiceRecord(MY_UUID) or createInsecureRfcommSocketToServiceRecord I get an error
java.io.IOException: read failed, socket might closed or timeout, read ret: -1
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); //Standard SerialPortService ID
socket = bsDevice.createInsecureRfcommSocketToServiceRecord(uuid);
socket.connect();
I am using an app, https://play.google.com/store/apps/details?id=no.nordicsemi.android.mcp&hl=en, that allows me to connect to the device but the characteristics that it shows is byte information and is cryptic.
How should I properly connect to the Arduino 101 BLE device?
Related
This question already has answers here:
How to unpair or delete paired bluetooth device programmatically on android?
(5 answers)
Closed 6 months ago.
I would like to delete paired bluetooth low energy devices with names that start with "ABC" on an Android phone programatically.
I am using Android studio.
If you are specific about BLE(bluetooth low energy),
To get all bonded devices you can write a method as.
public List<BluetoothDevice> getConnectedDevices() {
BluetoothManager btManager = (BluetoothManager)getSystemService(BLUETOOTH_SERVICE);
return btManager.getConnectedDevices(BluetoothProfile.GATT);
}
This return the list of BLE devices connected in GATT profile.
Fetch the name confirm if this the device you want to disconnet as:
List<BluetoothDevice> btdevices = getConnectedDevices();
for(int i=0;i<btdevices.size();i++)
{
//match your device here
Log.d("saurav"," BLE Name:"+btdevices.get(i).getName());
}
To disconnect you can simply call disconnect method. You need disconnect with gatt instance(Same gatt instance you used to connect the BLE device).
public void disconnect() {
if (gatt == null) {
return;
}
gatt.disconnect();
}
This will disconnect your BLE device. I have teste tshi personally and working for me.
I am trying to connect to remote device which needs the Bluetooth hardware address of that device to connect:
bdDevice = mBluetoothAdapter.getRemoteDevice(<bluetooth_hardware_address>);
How does one discover the bluetooth address of the remote device ?
These are the parameters of the remote device I already know - Serial Number, Equipment Number, VIN Number.
In iOS this is done using the properties of the remote device to get the Bluetooth hardware address, how is it done on Android.
I am trying to connect to remote device which needs the Bluetooth hardware address of that device to connect:
bdDevice = mBluetoothAdapter.getRemoteDevice(<bluetooth_hardware_address>);
How does one discover the bluetooth address of the remote device ?
These are the parameters of the remote device I already know - Serial Number, Equipment Number, VIN Number.
In iOS this is done using the properties of the remote device to get the Bluetooth hardware address, how is it done on Android.
UPDATE:
I can get address with the following code but this doesn't give serial number to match with --
String ui_serial_number = "000055557F9FC"; //I want to use this SN to connect.
BluetoothAdapter bluetoothAdapter;
Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
for (BluetoothDevice device : pairedDevices) {
// here you get the mac using device.getAddress()
//But there is nothing like device.getSerialNumber()
if(device.getSerialNumber().equals(ui_serial_number )) //What to do here?
{
//This is the <bluetooth_hardware_address> that I want to connect with.
bdDevice = mBluetoothAdapter.getRemoteDevice(device.getAddress());
break;
}
}
But there is nothing like device.getSerialNumber() to match with the serial number in the UI and connect.
When you scan for BLE devices by BluetoothAdapter.startLeScan() you will get all BluetoothDevices in callback. Then you can get each BluetoothDevice address BluetoothDevice.getAddress()
You can get a Set of all your bonded devices:
BluetoothAdapter bluetoothAdapter;
Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
for (BluetoothDevice device : pairedDevices) {
// here you get the mac using device.getAddress()
}
I am developing an application which should connect 2 Android devices through Bluetooth automatically. Let's say they are already paired. Is it possible to achieve that?
Of course it is possible. I'll make a short tutorial out of the documentation:
Start with the BluetoothAdapter - it is your Bluetooth manager.
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
If bluetoothAdapter is null, it means that this Android device does not support Bluetooth (It has no Bluetooth radio. Though I think it's rare to encounter these devices...)
Next, make sure Bluetooth is on:
if (!bluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, request_code_for_enabling_bt);
}
If it's not on, we start the activity which asks the user to enable it.
Let's say the user did enable (I guess you should check if he did, do it in your onActivityResult method). We can query for the paired devices:
Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
Then loop over them: for(BluetoothDevice device : pairedDevices) and find the one you want to connect to.
Once you have found a device, create a socket to connect it:
BluetoothSocket socket = device.createRfcommSocketToServiceRecord(YOUR_UUID);
YOUR_UUID is a UUID object containing a special ID of your app. Read about it here.
Now, attempt to connect (The device you are trying to connect to must have a socket created with the same UUID on listening mode):
socket.connect();
connect() blocks your thread until a connection is established, or an error occurs - an exception will be thrown in this case. So you should call connect on a separate thread.
And there! You are connected to another device. Now get the input and output streams:
InputStream is = socket.getInputStream();
OutputStream os = socket.getOutputStream();
and you can begin sending/receiving data. Keep in mind that both actions (sending and receiving) are blocking so you should call these from separate threads.
Read more about this, and find out how to create the server (Here we've created a client) in the Bluetooth documentation.
I have been asked to connect 2 concurrent bluetooth SPP devices (Serial port over bluetooth) on an Android tablet.
I used the bluetooth chat as a base to connect one, but I'm lost when it comes to connecting to 2 devices at the same time.
The goal is to collect data from two remote devices and compare the data.
The tablet can not act as a server, it must be client to those devices.
I looked around but did not found any source examples.
If someone could help...
Thanks
Cedric
Finally I cloned the class containing the connection threads and doubled the handler in the main activity.
I also doubled the menu in order to connect to the 2 devices and after a few tweakings, works like a charm !
it really is simple. just do everything 2 times.
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
bluetoothDevice = bluetoothAdapter.getRemoteDevice(btAddress1);
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
bluetoothSocket = bluetoothDevice.createInsecureRfcommSocketToServiceRecord(uuid);
Log.d(TAG, "start connect.");
bluetoothSocket.connect();
Log.d(TAG, "finished connect.");
Log.d(TAG, "getting second adapter");
bluetoothAdapter2 = BluetoothAdapter.getDefaultAdapter();
Log.d(TAG, "getting second adapter success, getting device 2");
bluetoothDevice2 = bluetoothAdapter2.getRemoteDevice(btAddress2);
Log.d(TAG, "getting second device success");
UUID uuid2 = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
bluetoothSocket2 = bluetoothDevice2.createInsecureRfcommSocketToServiceRecord(uuid2);
Log.d(TAG, "start connect 2.");
bluetoothSocket2.connect();
Log.d(TAG, "finished connect 2.");
I am programming for android 2.2 API Level 8 and I am trying to find out if there is currently a bluetooth headset connected when my application starts initially. I can not seem to find a solution to this issue.
Once my application is up and running I can listen for BluetoothDevice.ACTION_ACL_CONNECTED and BluetoothDevice.ACTION_ACL_DISCONNECTED as denoted here which works great mind you. But I can't figure out how to tell before the BroadcastReceiver is on if there is currently a bluetooth headset connected.
I have also figured out how to see if the device has any paired devices via the following.
BluetoothAdapter mBluetoothAdapter = null;
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter != null)
{
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if (pairedDevices.size() != 0)
{
// the device has paired devices
for (BluetoothDevice device : pairedDevices)
{
Log.i(TAG, "device name: " + device.getName());
Log.i(TAG, "device address: " + device.getAddress());
}
}
else
{
// no paired devices
Log.i(TAG, "no paired devices");
}
}
I am hoping that someone out there already has a solution to this issue for API Level 8. I do realize that in API 11 there are some new classes such as
BluetoothProfile and
BluetoothHeadset that could probably do this in 1 line of code, but again I am trying to do this in API Level 8
I am willing to try anything at this point.
Thanks in advance
-H
Following code works for me. First connect your BT device with android manually.
AudioManager am = getSystemService(Context.AUDIO_SERVICE)
if( am.isBluetoothA2dpOn() )
{
//bt device is connected with phone
}
else{
// no device is connected with phone
}
Unfortunately there is really no way to do this in Java on an unrooted device. If you have a device with root access and you use the NDK you can drop into the BlueZ programming layer and you can do it there, but with the publicly available Java API there is no way to do this.