I am using the below snippet to get the UUID for the devices that are in the range. Now i need to get only UUID's(services) for the devices that i select. How should i pass the name of the bluetooth device so that i get services for that device only.
if(BluetoothDevice.ACTION_UUID.equals(action))
{
BluetoothDevice device =intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Parcelable[] uuidExtra = intent.getParcelableArrayExtra(BluetoothDevice.EXTRA_UUID);
for (int i = 0; i < uuidExtra.length; i++) {
String abc=uuidExtra[i].toString());
}
}
else if(BluetoothDevice.ACTION_UUID.equals(action)){
Iterator<BluetoothDevice> itr = mDeviceList.iterator();
while (itr.hasNext()) {
// Get Services for paired devices
BluetoothDevice device = itr.next();
showToast("\nGetting Services for " + device.getName() + ", " + device);
if(!device.fetchUuidsWithSdp()) {
showToast("\nSDP Failed for " + device.getName());
}
}
}
Related
I'm working on a Bluetooth App with Android studio. Today, I get a weird issue.
I got 3 Bluetooth devices, a smartphone, a tablet, and another device but we don't really care about it.
I'm executing the same App on both devices but the smartphone is on Android 8.1 (API 27) and the tablet is on Android 4.0.4 (API 15).
On the smartphone, the app work well. When I scan for nearby devices, I get 4 different devices.
But there is the problem. On the tablet, when I scan for nearby devices, I got almost 10 times each devices detected by my smartphone. I don't really know why both devices are not discovering like each other. Maybe the Android version is the reason of that error.
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) { // discover devices
Scanned_devices = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
scanned_deviceName = Scanned_devices.getName();
scanned_macAddress = Scanned_devices.getAddress();
mDeviceList.add(scanned_deviceName + "\n" + scanned_macAddress);
Log.i("BT", scanned_deviceName + "\n" + scanned_macAddress);
Set<BluetoothDevice> pairedDevices = blueAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
try {
if (scanned_deviceName.equals(device.getName()) || scanned_macAddress.equals(device.getAddress())) {
Toast.makeText(getApplicationContext(), "Already Paired", Toast.LENGTH_LONG).show();
mDeviceList.remove(scanned_deviceName + "\n" + scanned_macAddress);
} //else {
//mDeviceList.add(scanned_deviceName + "\n" + scanned_macAddress);
//Log.i("BT", scanned_deviceName + "\n" + scanned_macAddress);
//}
}catch(Exception e)
{
Log.d("tag", "not working");
Toast.makeText(getApplicationContext(), "not working..", Toast.LENGTH_LONG).show();
}
}
}
Scanned_devices_ListView.setAdapter(new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, mDeviceList));
}
}
};
After trying a lot of thing to detect if the device already exist in my ArrayList, I tried this code :
if (!mDeviceList.contains(scanned_deviceName + "\n" + scanned_macAddress))
{
mDeviceList.add(scanned_deviceName + "\n" + scanned_macAddress);
Log.i("BT", scanned_deviceName + "\n" + scanned_macAddress);
}
This will check if an Array contain this String :
DeviceName
DeviceMac#
If the condition is true, the String is added to the ArrayList.
If it's false (there is another String with the same contents in the ArrayList), the String isn't added to the ArrayList.
I'm trying to get the name of the device that is connected to the Android phone running android Oreo.
I was searching for an answer for the past two days, and none of them worked. suggestions mostly returning ioexception-read-failed-socket-might-closed-bluetooth error
The question is, is there any way to make Query that returns the connected Bluetooth device?
These are the links and suggestion which not working:
IOException: read failed, socket might closed - Bluetooth on Android 4.3
In Android, how to get the profile of a connected bluetooth device?
list connected bluetooth devices?
I can get information about the device that is previously paired and trying to make a connection or a device trying to pair to the device. what I want is the name or the connection state of the currently paired and connected device.
here is the answer :
String name;
String address;
String threadName;
public void checkConnected()
{
BluetoothAdapter.getDefaultAdapter().getProfileProxy(this, serviceListener, BluetoothProfile.HEADSET);
}
private BluetoothProfile.ServiceListener serviceListener = new BluetoothProfile.ServiceListener()
{
#Override
public void onServiceDisconnected(int profile)
{
}
#Override
public void onServiceConnected(int profile, BluetoothProfile proxy)
{
for (BluetoothDevice device : proxy.getConnectedDevices())
{
name = device.getName();
address = device.getAddress();
threadName = Thread.currentThread().getName();
Toast.makeText(MainActivity.this, name+" " + address+ threadName, Toast.LENGTH_SHORT).show();
txtName.setText(name + " " + address);
Log.i("onServiceConnected", "|" + device.getName() + " | " + device.getAddress() + " | " + proxy.getConnectionState(device) + "(connected = "
+ BluetoothProfile.STATE_CONNECTED + ")");
}
BluetoothAdapter.getDefaultAdapter().closeProfileProxy(profile, proxy);
}
};
Have you tried this?
BluetoothServerSocket bluetoothServerSocket = bluetoothAdapter.listenUsingRfcommWithServiceRecord("abc", uuid);
BluetoothSocket bluetoothSocket = bluetoothServerSocket.accept();
BluetoothDevice device = bluetoothSocket.getRemoteDevice();
String deviceName = device.getName();
In my mobile app i have to get the connected BT address and start SPP connection. Is there any way to get the connected BT device address?
Following may be help you
public void getDeviceList(){
BluetoothAdapter mBlurAdapter= BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDevices = mBlurAdapter.getBondedDevices();
if (pairedDevices.isEmpty()) {
Log.e("DeviceActivity ",
"Device not founds");
return ;
}
for (BluetoothDevice devices : pairedDevices) {
Log.d("DeviceActivity", "Device : address : " + devices.getAddress() + " name :"
+ devices.getName());
}
}
Also don't miss to define permission
I need to get the details of all wireless devices that are connected to my wifi network. I'm getting IP, MAC addresses, somehow Vendors of device as well.
How do I get the device type? (i.e laptop, mobile, AC, Refrigerator) And host names?
InetAddress inetAddress = null;
for (int i = 0; i < 256; i++) {
try {
inetAddress = InetAddress.getByName("192.168.1." + i);
if (inetAddress.isReachable(30)) {
ConnectedDevice device = new ConnectedDevice();
device.setDeviceIPAddress(subnet + i);
device.setDeviceMACAddress(Utils.getMacAddressFromArpCache(inetAddress.getHostName()));
Log.d("Device", inetAddress.getHostName() + ", " + inetAddress.getHostAddress() + ", " + inetAddress.getCanonicalHostName() + ", " + device.getDeviceMACAddress());
}
} catch (Exception e) {
e.printStackTrace();
}
}
getHostName(), getHostAddress(), getCanonicalHostName() all 3 methods of inetAddress returning the ip address only. How do I get host names of connected devices on my network? What else should I do to get all the possible details of devices? Please guide me.
I am trying to connect two Android devices with API 14+ via USB OTG cable,
but I am getting null values when I was accessing two APIs like followings
mUsbManager.getDeviceList();
mUsbManager.getAccessoryList();
Please help me sharing your ideas or any example of sample apps if you have any.
Are you requesting the permission through the intent to even see what is attached?
Are you defining your own custom type and communication layer or trying to do so over adb....? More info here might be good. For now though, you need to know if you can even see the device.
This problem is somewhat defined in the AOA v2 page : https://source.android.com/devices/accessories/aoa2.html
Text copied from : http://mobilemerit.com/android-app-for-usb-host-with-source-code/
private void checkInfo() {
manager = (UsbManager) getSystemService(Context.USB_SERVICE);
/*
* this block required if you need to communicate to USB devices it's
* take permission to device
* if you want than you can set this to which device you want to communicate
*/
// ------------------------------------------------------------------
mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(
ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
registerReceiver(mUsbReceiver, filter);
// -------------------------------------------------------------------
HashMap<string , UsbDevice> deviceList = manager.getDeviceList();
Iterator<usbdevice> deviceIterator = deviceList.values().iterator();
String i = "";
while (deviceIterator.hasNext()) {
device = deviceIterator.next();
manager.requestPermission(device, mPermissionIntent);
i += "\n" + "DeviceID: " + device.getDeviceId() + "\n"
+ "DeviceName: " + device.getDeviceName() + "\n"
+ "DeviceClass: " + device.getDeviceClass() + " - "
+ "DeviceSubClass: " + device.getDeviceSubclass() + "\n"
+ "VendorID: " + device.getVendorId() + "\n"
+ "ProductID: " + device.getProductId() + "\n";
}
textInfo.setText(i);
}
private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION_USB_PERMISSION.equals(action)) {
synchronized (this) {
UsbDevice device = (UsbDevice) intent
.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (intent.getBooleanExtra(
UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
if (device != null) {
// call method to set up device communication
}
} else {
Log.d("ERROR", "permission denied for device " + device);
}
}
}
}
};