Android Bluetooth connection With MAC Address - android

Is it possible to create a Bluetooth connection with only a MAC address of the module?
Example of my code
private static String address = "00:15:FF:F2:19:5F";
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice device = BluetoothAdapter.getRemoteDevice(address);
new ReadBluetoothTask().execute(new Object[] ReadBluetoothTask.test.read, device});
But i think something's wrong with the getRemoteDevice() method.
Thanks

Related

How to get only present connected Bluetooth device name in android

I am trying get the connected Bluetooth device name in android.
Done like below ,
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
String name = mBluetoothAdapter.getName();
Log.d(TAG,"name--->"+name);
but I am getting my device name.
You can use Android BluetoothManager
following functions you can make use of :
getConnectionState (BluetoothDevice device, int profile)
getConnectedDevices (int profile) //get fetch connected device's info
Example :
BluetoothManager btManager = (BluetoothManager)
mContext.getSystemService(Context.BLUETOOTH_SERVICE);
List<BluetoothDevice> devices =
btManager.getConnectedDevices(BluetoothProfile.GATT);
for(BluetoothDevice device : devices) {
// you will get each device's info here.
}
If you have the mac-address of the connected device then you can get the device by
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(mac-address);
And to get the name of the connected device
String connectedDeviceName = device.getName()

Get RSSI Value of Android Explicitly Paired Devices

I have paired my device to the Android phone explicitly, via an app. This device is a MI-Band 2, and it is paired using the MI-FIT app. I am currently connecting to bluetooth devices using this code:
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
int rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, Short.MIN_VALUE);
However, the problem is that if the MI-band is connected via the mi-fit app, I cannot get the RSSI value from the MI-Band.
Is it possible to get the RSSI value of explicitly paired devices in Android? If so, what am I doing wrong?
Thanks
Edit: It seems like I can't get RSSI values of any connected devices. For example, if Android phone A is connected to Android Phone B, then I can't read the RSSI values when trying to read via the above code from Phone A.
If you know for sure the bluetooth name of device to be connected, maybe you can try using BluetoothAdapter.LeScanCallback to get noticed when it's scanned. Or do you really need to tell scanned or connected apart?
BluetoothManager bleManager = (BluetoothManager) getApplicationContext().getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter bleAdapter = bleManager.getAdapter();
bleAdapter.startLeScan(leScanCallback);// find match device
BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() {
#Override
public void onLeScan(final BluetoothDevice device, final int rssi, byte[] scanRecord) {
if (device.getName() == null || !device.getName().equals("your bluetooth device name")) {
return;
}
// here you can get rssi of specified bluetooth device if it's available.
// and try to connect it programmatically.
connect(device.getAddress());
}
};

Bluetooth serial communication between android and arduino uno

I am very much new to phonegap. I need to send and recieve data via arduino and android mobile.I have minimum or better to say zero knowledge of how to do this.
Please guys do help me.Can't understand a thing by following this link.
https://github.com/don/BluetoothSerial
Please give step wise description if you guys have done this.
first you need to buy a Bluetooth shield for your Ardunio uno if you do not have one. The Bluetooth shield should have instructions on how to connect it to the Ardunio uno to use the serial ports. Android has an excellent sample application that is distributed with the SDK, it is called BluetoothChat, you should be able to find it easy. I modified the BluetoothChatService.java file to communicate with the Arduino board with few simple modifications to the code where you can use the app to connect to the Arduino board or any other Bluetooth device., here they are.
I added this at the start of the class, the second UUID is used to connect to the Arduino board.
// Name for the SDP record when creating server socket
private static String NAME = null;
private static final String NAME1 = "BluetoothChat";
private static final String NAME2 = "itead";
// Unique UUID for this application
private static UUID MY_UUID = null;
private static final UUID MY_UUID1 = UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66");
private static final UUID MY_UUID2 = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
Also, I modified this method,
/**
* Start the ConnectThread to initiate a connection to a remote device.
* #param device The BluetoothDevice to connect
*/
public synchronized void connect(BluetoothDevice device) {
if (D) Log.d(TAG, "connect to: " + device);
if(device.getName().indexOf("itead")!=-1)
{
MY_UUID = MY_UUID2;
NAME = NAME2;
}
else
{
MY_UUID = MY_UUID1;
NAME = NAME1;
}
// Cancel any thread attempting to make a connection
if (mState == STATE_CONNECTING) {
if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;}
}
// Cancel any thread currently running a connection
if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;}
// Start the thread to connect with the given device
mConnectThread = new ConnectThread(device);
mConnectThread.start();
setState(STATE_CONNECTING);
}
You should be to connect and communicate with the Arduino board with the current few changes I made to the sample app.
Hope that works for you.

jsr 82 connect connection is not created (failed or aborted) sometimes appear when I try to connect to Cashregister via Bluetooth rfcomm

I'm trying to connect to my cashregister via rfcomm bluetooth on my phone Lenovo A369i using reflection like this:
BluetoothAdapter ba = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice device = null;
String macAddr = "00:01:90:ED:27:73";
for (BluetoothDevice bd : ba.getBondedDevices())
{
if (bd.getAddress().equals(macAddr))
{
device = bd;
break;
}
}
Method htcMethod = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
htcMethod.invoke(device, Integer.valueOf(1));
And sometimes I'v get error "jsr 82 connect connection is not created (failed or aborted)"
I'v already add all bletooth permissions to androidManifest.xml
I don't know what to do, please help me...

How to detect hands free garniture on Android 2.2+?

I want to know when user has connected hands free accessories and hasn't blocked calls\sms. Is it possible to know when it is connected via hardware ports or bluetooth?
Try this in your onCreate or onResume.
BluetoothAdapter myLocalAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice garniture;
Set<BluetoothDevice> connectedDevices = myLocalAdapter.getBondedDevices();
for (BluetoothDevice device : connectedDevices){
String name = device.getName();
//... check for the name you want
if( name.equals("whatnameisit"){
garniture = device
}
}
if (garniture != null){
// yay we found it, lets do our work with the device here
}

Categories

Resources