I have to connect at least two devices to one Android device. But I don't know how to connect multiple connections and multiple command send. plz help me... i really don't know that....
I just can connect one BLE device to One Android device.
first, i connected two devices(same UUID, different address) in one Service like example
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice("aa:12:12:33");
mGatt = device.connectGatt(mContext, false, mGattCallback);
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice("bb:18:66:44");
mGatt = device.connectGatt(mContext, false, mGattCallback);
i can listen to each device action(they have a button), GattCallback's callback comes from two devices action, but i can't send BluetoothGatt.writeCahracteristic to two devices.
I'm not sure, but i solved this problem. Before i tried to use one Service(Android Component) but i used Service each BLE Device. but i think this is not solution, because if i have to more BLE device, I should to prepare maximum Service Component.
Related
I want the connected device to get BluetoothDevice device information without scanning Bluetooth, so I store all Bluetooth-connected devices.
However, while other devices behave as expected, one type of Bluetooth devices will lose their paired information when the phone is rebooted.
Do anyone know why this may happen or have an alternative way to obtain Bluetooth Device information without scanning?
You can use BluetoothAdapter.
BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(mac);
device.connectGatt(mContext, false, mGattCallback);
// TODO your code
If the bluetooth device is not around, in BluetoothGattCallback's onConnectionStateChange will report BluetoothGatt.STATE_DISCONNECTED.
BluetoothDevice's creator has packages scope. But the source code of BluetoothAdapter's getRemoteDevice is:
public BluetoothDevice getRemoteDevice(String address) {
return new BluetoothDevice(address);
}
I'm trying to connect to a specific device using my Android APP, until now what I was able to do is get the paired items doing this :
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Set < BluetoothDevice > pairedDevices = bluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device: pairedDevices) {
mDeviceName.add(device.getName());
mDeviceMAC.add(device.getAddress());
}
}
bluetoothClass.setDeviceName(mDeviceName);
bluetoothClass.setDeviceMac(mDeviceMAC);
Where I get the MAC and the Device name of all of my paired devices. The thing that I would like to do is when I select one it connects to the Bluetooth device.
EXAMPLE
On a Samsung S4 when I turn on the Bluetooth it popups a Dialog whitch contains all of my paired devices and when I click on anyone it connects (I've i'm able to ...) so basically I want to do this, since now I've been getting the paired devices (I don't know if it's the best way to get that but it does) and then when user click on anyone it connects to the device.
It's something like this question but it's unfortunately unanswered.
It's impossible to give you an example within this format, so I have provided you
with a good sample and helpful links to help you understand the sample.
I recommend you follow the steps I have provided and then, when you have
specific problems, you can bring it here, with the code snippet you are having
difficulty with.
I recommend you use download this sample code:
http://developer.android.com/samples/BluetoothChat/index.html
If you haven't already, it's good to study this:
http://developer.android.com/reference/android/bluetooth/BluetoothDevice.html
This is a good tutorial and they have many tutorials:
http://www.tutorialspoint.com/android/android_bluetooth.htm
You will need the following permissions in your manifest:
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
This is one intent that is advisable to use, to check to see if BT is enabled:
if (!mBluetoothAdapter.isEnabled()) {
android.content.Intent enableIntent = new android.content.Intent(
android.bluetooth.BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
}
and to make your device discoverable to other devices:
if (mBluetoothAdapter.getScanMode() !=
android.bluetooth.BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
android.content.Intent discoverableIntent =
new android.content.Intent(
android.bluetooth.BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(
android.bluetooth.BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,
300); // You are able to set how long it is discoverable.
startActivity(discoverableIntent);
}
As I mentioned in my answer here:
You can avoid using an intent to search for paired devices. When
connecting to a device that is not paired, a notification will pop up
asking to pair the devices. Once paired this message should not show
again for these devices, the connection should be automatic (according
to how you have written your program).
I use an intent to enable bluetooth, and to make my device
discoverable, I then set up my code to connect, and press a button to
connect. In your case, you will need to ensure your accessories are
discoverable also. In my case I use a unique UUID, and both devices
must recognise this to connect. This can only be used if you are
programming both devices, whether both are android or one android and
one other device type.
You will need to understand how to use sockets, this is how the devices communicate.
I recommend studying these two links:
http://developer.android.com/reference/android/bluetooth/BluetoothSocket.html
http://developer.android.com/reference/android/bluetooth/BluetoothServerSocket.html
Sockets are used to set up connections between devices. There will be a server socket and device sockets (determined by many factors, the programmer, the actual devices). The server socket listens for incoming connections, when a connection is accepted the devices connect, each with a simple socket.
I am not sure how much you know about threading.
The connection needs to be managed with threads:
http://developer.android.com/guide/components/processes-and-threads.html
http://android-developers.blogspot.com.au/2009/05/painless-threading.html
The connection between the devices is managed on threads separate from the User
Interface thread. This is to prevent the phone from locking up while it is
setting up, seeking and making a BT connection.
For instance:
AcceptThread - the thread that listens for a connection and accepts the connection (via the serversocket). This thread can run for an extended time waiting for a device to connect with.
ConnectThread - the thread that the device connecting to the server uses to connect to the serversocket.
ConnectedThread - this is the thread that manages the connection between both sockets.
Let me know if this helps you.
I am working on BLE (bluetooth) in Android. I have found information via Google. It seems Android can be connected to multiple BLE device by using multiple BluetoothGatt like the following pseudo code.
I have multiple BLE devices. I think I need to define multiple BluetoothGatt parameters first.
private BluetoothGatt mBluetoothGattA = null,
mBluetoothGattB = null , mBluetoothGattC = null;
The First BLE device connection.
final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address_A);
mBluetoothGattA = device.connectGatt(this, false, mGattCallback);
Try to connected to the second BLE device.
final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address_B);
mBluetoothGattB = device.connectGatt(this, false, mGattCallback);
Am I right? If I connect to the 6 BLE devices, should I define 6 BluetoothGatt parameters?
Is there a smart way to connect to multiple BLE device ?
Is not necesary multiple BluetoothGattCallback. Accidentally I connect two BLE devices using the same BluetoothGattCallback and works fine .
In the BluetoothGattCallback you must to know the address of the device that send data.
For example you can use :
gatt.getDevice().getAddress();
Hope this help
You are going to handle each BLE device by creating each BluetoothGattCallback for each one (maximum 7 right now). for example like:
private final BluetoothGattCallback oneGattcallback = new BluetoothGattCallback() ...
private final BluetoothGattCallback twoGattcallback = new BluetoothGattCallback() ...
then try to connect
mBluetoothGattA = deviceA.connectGatt(this, false, oneGattcallback );
and mBluetoothGattB = deviceB.connectGatt(this, false, twoGattcallback ); just like that. you will find a lot examples handle one connection and just develop more for multiple connection.
I try to develop a simple android app with a ble device.
I found many source code from the Intenet. However, both of them were start from scanning a list of available ble device.
As I have MAC address of the device,UUID of service and characteristic.
How can I connect to the known device and read one specific characteristic directly??
To connect a specific Bluetooth Device which has details like MAC address of the device, UUID of service and characteristic etc. you already know. To do this you need a BluetoothDevice object to make a call like this:
yourBluetoothDevice.connectGatt(getApplicationContext(), false, bleGattCallback);
So for this (yourBluetoothDevice) you have to start the scan at first to get same device object to connect by comparing it's MAC address. However, you got that same device object in onLeScan callback just stop scanning and make a connection with the same device.
Note: Making a connection should be on UIThread(Either using Handler, runOnUIThread or mainlooperThread) otherwise it will give issue in some devices for 'Fail to register callback'
Here yourBluetoothDevice is the device object reference with you want to make a connection.
bleGattCallback is the registered new BluetoothGattCallback() callback for connection status, discovered services, characteristics read and write etc.
Take a look at createInsecureRfcommSocketToServiceRecord. Something like this:
UUID uuid = UUID.fromString("<Your UUID>");
BluetoothSocket socket = yourBLEDevice.createInsecureRfcommSocketToServiceRecord(uuid);
Method m = yourBLEDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
socket = (BluetoothSocket) m.invoke(yourBLEDevice, 1);
If you already know the mac address, you can try something like below:
final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(mMacAddress);
final BluetoothGatt mGatt = device.connectGatt(getApplication(), false, gattCallback);
Is it possible to connect with other Bluetooth LE device with out scanning.
I am working on app when bluetooth is ON and then automatically received the notification when I enter in any marketPlace that Beacon device(basically Bluetooth LE) is in your range. without my scanning Bluetooth Le. My Bluetooth is just ON. no scanning.
Because our requirements are that bluetooth doesn't scan just on, when ever new BLE is in range show alert or any notification.
I implement some scan method
startScan(){}
stopScan(){}
#Override
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {}
but i don't want that i directly want to get connection message.
Please help me in form of pieces of code and also with little bit explanation
Thanks
You can use BluetoothAdapter.
BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(mac);
device.connectGatt(mContext, false, mGattCallback);
// TODO your code
If the bluetooth device is not around, in BluetoothGattCallback's onConnectionStateChange will report BluetoothGatt.STATE_DISCONNECTED.
BluetoothDevice's creator has packages scope. But the source code of BluetoothAdapter's getRemoteDevice is:
public BluetoothDevice getRemoteDevice(String address) {
return new BluetoothDevice(address);
}
Do you just want to discover devices that support a particular service? There is an overload startLeScan(UUID[], ...), where you can pass the UUID's of the services that you are interested in.
Otherwise, if you just want to connect to a device of a known BT address, you maybe able to create a Bluetooth device object with that address and call connectGatt() on it. Just an idea, not sure if this would work :-)
Creating a bluetooth device object with address is not possible!
BluetoothDevice's creator has packages scope. so you can not create BluetoothDevice.
And although BluetoothDevice implements Parcelable, it cannot be created from a file.