I am having an Android-App [1] which I partly want to port to google-glass - this app uses bluetooth rfcomm. Now I am facing the following problem: when I use my connection code I see a pairing dialog on glass - showing me a large number and asks for a tap to confirm. But this is strange - as I usually have to enter my 4 digit pin on the phone - also I am getting auth problems ( smells like it is caused by not letting me enter the PIN )
Anyone using bluetooth-rfcomm on google-glass?
[1] https://github.com/ligi/DUBwise
I was having the exact problem like this! In this post I put my complete solution to this problem.
But basically the pairing is done like this:
In the BroadcastReceiver
if(BluetoothDevice.ACTION_PAIRING_REQUEST.equals(action)){
BluetoothDevice device = ListDev.get(selectedDevice);
byte[] pinBytes = getStrFromName(device.getName(),7,11).getBytes(); // My devices had their own pin in their name, you can put a constant pin here or ask for one...
try {
Method m = device.getClass().getMethod("setPin", byte[].class);
m.invoke(device, pinBytes);
try {
device.getClass().getMethod("setPairingConfirmation", boolean.class).invoke(device, true);
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
So the pin is set automatically in this example, but you can always ask for a pin to the user.
Hope it helps!
Related
I had make an application related to Bluetooth connection in Android, every time when I enable Bluetooth, it shows a confirmation dialog that make your device discover able no near by device.
Is there any way to make device discover able without confirmation dialog.
I had used this:
Intent Intent= new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
Intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
I found this helpful:
try {
Method bluetoothDeviceVisibility;
bluetoothDeviceVisibility = bluetoothAdapter.getClass().getMethod("setScanMode", int.class, int.class);
bluetoothDeviceVisibility.invoke(bluetoothAdapter, BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, 0);
} catch (Exception e) {
e.printStackTrace();
}
I'm working with connecting and retrieving data from IP in android. To check whether IP is online before retrieving data, I'm pinging IP using InetAddress.getByName(hostName).isReachable(20000). It's working fine for LAN, But for WAN, getting timeOut.
Any help is appreciated!!
try {
boolean b = InetAddress.getByName(hostname).isReachable(40000);
}
catch (Exception e)
{
e.printStackTrace();
}
you have to add this 20000 milllisecod to 40000 milliseconds..
so this is paramter if you not reach your network at 20000 then you got timeout..
I hope its useful to you.
I wrote simple application, for my Galaxy SII, for permanent connection with remote server. Ones for 5 second it sends and receives data. While application is working nobody can't call to me. Network answers him - interlocutor is unavailable.
The same happend me when I use mail client like K-9. It doesn't matter I use GPRS or 3G connection.
What is a main rule (if is it?) to construct internet application to avoid this problem (I mean problem with ordinary incomming phone connection)?
My ordinary code for sending data (in remote service) is like this:
While (condition)
{
try {
Thread.sleep(500);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
out.write(data + "\n");
out.flush();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Regards,
Artik
First, if you want to wait for 5 seconds you should use Thread.sleep(5000); not Thread.sleep(500); which is half a second.
Second, consider sending your data to the server using Timer instead of Thread.sleep() and while(condition)
After modifying your code with the above suggestions, try to test from the emulator and simulate a phone call.
I have been strugling with a Bluetooth project on Android for weeks. Does anyone know where I can go to see the actual code that's used by Google to make their Bluetooth pairing and connection logic work?
I have been through all the documentaiton, the BluetoothChat application (which doesn't work as advertised ... tried it on 3 different handsets), as well as a bunch of other sites on the net, but still no luck. I need to get an app up and running on 2.1 or higher.
Any advice or help is greatly appreciated.
Yes the Bluetooth project didn't work for me also because the code for socket connection is not working
// Get a BluetoothSocket for a connection with the
// given BluetoothDevice
try {
if (secure) {
tmp = device.createRfcommSocketToServiceRecord(
MY_UUID_SECURE);
} else {
tmp = device.createInsecureRfcommSocketToServiceRecord(
MY_UUID_INSECURE);
}
} catch (IOException e) {
Log.e(TAG, "Socket Type: " + mSocketType + "create() failed", e);
}
this is not working ...
replace this by the following code
BluetoothDevice hxm = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(device.getAddress());
Method m;
m = hxm.getClass().getMethod("createRfcommSocket", new Class[]{int.class});
socket = (BluetoothSocket)m.invoke(hxm, Integer.valueOf(1));
Ah, if you're having issues with application level code I'm not sure staring at the Bluetooth manager source will be much help, but here you go: https://android.googlesource.com/platform/packages/apps/Bluetooth the Bluetooth manager app code.
I'll re-iterate it: this is honestly probably not going to be helpful for what you want. You should be able to get a reasonably working Bluetooth app without having to look at this.
EDIT: if you want the code that implements the Bluetooth packages (android.bluetooth), see https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/bluetooth for that.
You can browse all the android.bluetooth package around here:
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/bluetooth/BluetoothClass.java#BluetoothClass
Is there a way, using the Android SDK, to programmatically connect to an already-paired Bluetooth device?
In other words: I can go into Settings -> Wireless & networks -> Bluetooth settings, and tap the device (listed as "Paired but not connected"), at which point it will connect. I'd like to be able to do this programmatically, but don't see a way to do this.
I see the options to create an RFCOMM socket, and for a SPP device, I'm assuming that'll do the connection part as well, but for an A2DP device, where the actual data transfer will be handled by the OS rather than by my app, I think that's not applicable?
Okay, since this was driving me crazy, I did some digging into the source code and I've found a 100% reliable (at least on my Nexus 4, Android 4.3) solution to connect to a paired A2DP device (such as a headset or Bluetooth audio device). I've published a fully working sample project (easily built with Android Studio) that you can find here on Github.
Essentially, what you need to do is:
Get an instance of the BluetoothAdapter
Using this instance, get a profile proxy for A2DP:
adapter.getProfileProxy (context, listener, BluetoothProfile.A2DP);
where listener is a ServiceListener that will receive a BluetoothProfile in its onServiceConnected() callback (which can be cast to a BluetoothA2dp instance)
Use reflection to acquire the connect(BluetoothDevice) method on the proxy:
Method connect = BluetoothA2dp.class.getDeclaredMethod("connect", BluetoothDevice.class);
Find your BluetoothDevice:
String deviceName = "My_Device_Name";
BluetoothDevice result = null;
Set<BluetoothDevice> devices = adapter.getBondedDevices();
if (devices != null) {
for (BluetoothDevice device : devices) {
if (deviceName.equals(device.getName())) {
result = device;
break;
}
}
}
And invoke the connect() method:
connect.invoke(proxy, result);
Which, at least for me, caused an immediate connection of the device.
the best way I found to solve my problem was finding out that I can create a button that brings up the Bluetooth Settings screen. I didn't realize you could do this, or I would have from the beginning.
startActivity(new Intent(Settings.ACTION_BLUETOOTH_SETTINGS));
if the device is already paired , then you can use
if(device.getBondState()==device.BOND_BONDED){
Log.d(TAG,device.getName());
//BluetoothSocket mSocket=null;
try {
mSocket = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e1) {
// TODO Auto-generated catch block
Log.d(TAG,"socket not created");
e1.printStackTrace();
}
try{
mSocket.connect();
}
catch(IOException e){
try {
mSocket.close();
Log.d(TAG,"Cannot connect");
} catch (IOException e1) {
Log.d(TAG,"Socket not closed");
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
for the MY_UUID use
private static final UUID MY_UUID = UUID.fromString("0000110E-0000-1000-8000-00805F9B34FB");
the above code snippet is just to connect your device to an A2DP supported device.
I hope it will work.
I used the code here as a starting point for this functionality in my app: http://developer.android.com/guide/topics/wireless/bluetooth.html#ConnectingDevices
Once the device is paired, the app has no problem connecting the two devices together programmtically.