I want to know is there any way to determine model of smartphone connected to my android device via bluetooth programmatically? I know how to make bluetooth connection between two android devices, but how to request information of smartphone's model? Thanks.
Use the BluetoothManager for this task:
List<BluetoothDevice> connectedDevices = this.getSystemService(Context.BLUETOOTH_SERVICE)
.getConnectedDevices(BluetoothProfile.GATT); //or BluetoothProfile.GATT_SERVER
for(BluetoothDevice bd : connectedDevices){
String remoteName = bd.getName();
//do something
}
Related
I'm writing a flutter app which uses bluetooth Connectivity. I want to know if my device is connected to another device via bluetooth. I want to ask if there is something like Internet connectivity with bluetooth if there isn't I want to know if there is a method to replace it.
My app has as role to open bluetooth is it's off I did it and then if the device bluetooth state is connected or not to another device.
void checkconnectivity() async{
//check bluetooth connectivity
}
You can use this package bluetooth
Example:
/// Create a connection to the device
var deviceConnection = flutterBlue.connect(device).listen((s) {
if(s == BluetoothDeviceState.connected) {
// device is connected, do something
}
});
/// Disconnect from device
deviceConnection.cancel();
I'm making an Android application that scan all device in the room and connect/pair to the device automatically or not, depending on the name prefix.
I tried some examples and the scan result return only unpaired devices. So I need that the result of the scan return also already paired devices. Is it possible on Android ? (I already done this on windows)
Yes. You need to get the local bluetooth adapter
Adapter = BluetoothAdapter.getDefaultAdapter();
get paired devices into a private set Bluetooth device
devicepair = Adapter.getBondedDevices();
And then from that point it should be a simple for loop to add device pair to a list view that shows your bluetooth devices
for(BluetoothDevice : devicepair)
//your array name or whatever inside the for loop
listviewarray.add(devicepair.getName());
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);
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'm trying to use my Android phone as a handsfree kit (like the one for cars) in order to connect to another phone (any phone) and perform some handsfree functionality like (answer an incoming call, reject,.. etc) which can be done using the AT commands for handsfree profile.
For that, I'm using the well-known Bluetooth chat App, and reflection work around in order to establish a connection with any device:
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
tmp = (BluetoothSocket) m.invoke(device,1);
However, in order to achieve the handsfree functionality and understand the AT commands that I'm sending, the connected phone needs to be over the handsfree profile which uses the UUID: "0000111f-0000-1000-8000-00805F9B34FB"
Therefore, is there a way to achieve a connection to the handsfree profile?
Thanks!
You should only use this code when you have no other choice. The 1 in this code is the RFCOMMÂ port. Each service has it's own RFCOMM port. This port is usually random between 1 and 31. You need to know which port the service (here handsfree profile) is using on the device that you want to connect to. You have to use the createRfcommSocketToServiceRecord method from the BluetoothDevice object to do this:
try { clientSocket = bluetoothDevice.createRfcommSocketToServiceRecord( serviceUUID ); }
catch (IOException e)
{
// handle error
}
This code is the correct way to use Bluetooth and should replace the one you're using.