I currently purchased the 3DR bluetooth module for pixhawk to transfer telemetry data to an android phone. I am able to connect to the device, i.e. the bluetooth module turns solid red. However, the android program says that the phone and pixhawk are not connected. Here is my current connection setup.
protected void updateConnectedButton(Boolean isConnected) {
Button connectButton = (Button)findViewById(R.id.btnConnect);
connectButton.setText(isConnected ? "Disconnect" : "Connect");
}
public void onBtnConnectTap(View view) {
if(drone.isConnected()) {
drone.disconnect();
} else {
Bundle extraParams = new Bundle();
extraParams.putInt(ConnectionType.EXTRA_USB_BAUD_RATE, DEFAULT_USB_BAUD_RATE); // Set default baud rate to 57600
//connect with usb
//ConnectionParameter connectionParams = new ConnectionParameter(ConnectionType.TYPE_USB, extraParams, null);
ConnectionParameter connectionParams = new ConnectionParameter(ConnectionType.TYPE_BLUETOOTH,extraParams,null);
drone.connect(connectionParams);
}
try {
Thread.sleep(8000);
} catch(InterruptedException e) {
}
updateConnectedButton(drone.isConnected());
}
If I remove the USB Baud rate setting, the red light on the device keeps blinking when I attempt to connect. I added a sleep because the bluetooth module takes a while to connect. The documentation and examples don't talk much about bluetooth connections. Any ideas what I am doing wrong?
Related
so i'm currently developing an android service which implements the HFP profile for later use with a gui , i was able to successfully and easily implement the RFCOMM part where the AT commands like ATA(accep call) are sent , but i am stuck with accepting the audio SCO Connection on the app . so basically im testing with an Iphone AG Role and an android tablet HF Role which runs my app , to open the SCO connection i have tried calling AudioMAnager.startBluetoothSco(); without any luck , and even made a bluetooth SCO socket server in C using the ndk , which listens for a connection. but the actual problem is that the Iphone doesnt seem to try to connect with the sco socket , so i dumped the trafic from the android tablet and saw that when the Iphone requests the SCO connection the android hci automatically responds with Reject of reason: Connection Rejected due to Limited Resources (0x0d) , no matter what i do, maybe im missing something? any ideas? thanks. forgot to mention that both devices are paired using the os settings app and the connection is established by connecting to the android tablet from the native settings app of ios.
AudioHandler.java
public class AudioHandler implements Runnable{
static
{
System.loadLibrary("libsco");
}
#Override
public void run()
{
AudioManager amanager = (AudioManager) Common.APPCONTEXT.getSystemService(Context.AUDIO_SERVICE);
amanager.setMode(AudioManager.MODE_IN_COMMUNICATION);
amanager.startBluetoothSco();
amanager.setBluetoothScoOn(true);
if(amanager.isBluetoothScoOn())
{
int status= this.SCOINIT();
Log.d("SCO","SCOFinished");
}
/*while(Common.isCallActive)
{
play(stream.readbytes());
}
this.SCOCLOSE();
amanager.stopBSCO();
*/
}
private native int SCOINIT();}
libsco.c
void init() {
struct sockaddr_sco addr = {0}, remoteadress = {0};
int SCOServer, SCOClient;
SCOServer = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_SCO);
if (SCOServer < 0) {
__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, "SCO socket create failed.");
print("SCO socket create failed.");
}
addr.sco_family = AF_BLUETOOTH;
bacpy(&addr.sco_bdaddr, BDADDR_ANY);
if (bind(SCOServer, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
print("failed to bind sco.");
}
if (listen(SCOServer, 1)) {
print("Listening Failed!!");
} else {
print("Listening for SCO connection.");
}
socklen_t addrlength = sizeof(remoteadress);
SCOClient = accept(SCOServer, (struct sockaddr *) &remoteadress, &addrlength);
if (SCOClient < 0) {
print("Accept Failed!!");
close(SCOClient);
} else {
print("Conected.\n");
close(SCOClient);
}
close(SCOServer);
}
everything runs without errors and i can see the "LIstening for SCO Connection " line, but it never accepts because the android hci rejects the connection before anything can be done...
screenshot from wireshark
we are using symbol ls4278 bluetooth scanner to integrate in android application. It is connected as keyboard and types scanned barcode in any edittext field...
After scanner OnKeyUp event is called.
public override bool OnKeyUp(Keycode keyCode, KeyEvent e)
{
..
}
I was searching documentation and android sdk, but I can't found such one. But for LI4278 they have android sdk here : https://www.zebra.com/us/en/support-downloads/scanners/general-purpose-scanners/li4278.html
here is also documentation for sdk but LS4278 is not in supported device list.
Does anyone implemented LS4278 scanner in android devices?
The LS4278 product page is here: https://www.zebra.com/us/en/support-downloads/scanners/general-purpose-scanners/ls4278.html and lists support for the "Windows Scanner SDK" ONLY. The LS4278 was discontinued on September 24th 2012 so I am not surprised it does not have Android support. As you say, its successor, the LI4278 does have Android support. As the other answer states, if you want more control over how you receive data then I suggest trying SPP if the scanner supports it.
If it works as a bluetooth keyboard, then no support is needed. Just capture the key events, and react to the data when enter is pressed. Its just a mediocre experience and can mess with on screen keyboards and stop them from using an actual bluetooth keyboard. If the scanner supports SPP, you can pretty trivially parse the scan data out of it via bluetooth serial (I did this about 2 weeks ago).
BluetoothAdapter bta = BluetoothAdapter.getDefaultAdapter();
if(bta != null) {
Set<BluetoothDevice> devices = bta.getBondedDevices();
for (final BluetoothDevice device : devices) {
BluetoothClass btClass = device.getBluetoothClass();
if (btClass.getMajorDeviceClass() == 0x1f00) {
//Only look at devices which are considered uncategorized, so we don't screw up any bt headset, leyboard, mouse, etc
new DeviceThread(device).start();
}
}
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
registerReceiver(new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.BOND_NONE);
if (state == BluetoothDevice.BOND_BONDED) {
new DeviceThread(device).start();
} else if (state == BluetoothDevice.BOND_NONE) {
DeviceThread thread = threadMap.get(device.getAddress());
if (thread != null) {
thread.interrupt();
}
}
}
}, filter);
}
private class DeviceThread extends Thread {
private BluetoothDevice device;
public DeviceThread(BluetoothDevice device) {
this.device = device;
threadMap.put(device.getAddress(), this);
}
#Override
public void run() {
try {
BluetoothSocket socket = device.createInsecureRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
socket.connect();
InputStream inputStream = socket.getInputStream();
while (!Thread.interrupted() && socket.isConnected()) {
inputStream.skip(5);
String data = "";
do {
int code = inputStream.read();
char character = (char) code;
data = data + character;
} while (inputStream.available() > 0);
data = data.substring(0, data.length() - 2);
if (scannerEventListener != null) {
scannerEventListener.onScan(data);
}
}
} catch (IOException ex) {
ex.printStackTrace();
}
Log.d("GABE", "Exiting thread");
}
}
This code will register for bluetooth devices being paired, then check and see if they're unknown device types (scanners don't have a device class). If so, it will start a thread to listen for that device. When its unbonded, it will interrupt that thread. On the thread it opens up a SPP connection to the device and waits for input. When it gets it, it parses the input and sends the result to a listener.
For this to work, the scanner needs to be in SPP mode. Some scanners support it, some don't, and how to set it into that mode varies (the one on my desk has a control barcode I need to scan to set the mode). Generally I would code for it to accept either type of input- hardware keyboard mode or SPP.
I would like to manually connect a bluetooth device with its MAC address because it is faster and I know exactly which MAC to connect.
I use this method to get the BluetoothDevice : http://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#getRemoteDevice%28byte[]%29
But the Android doc does not say if Android ensure that the device is in range before creating the BluetoothDevice object.
Do you have this information ?
My code can automatically connect the device, and I would like to check if the target is in range before trying to connect, but without perform a large scan (which can be long...)
When local device connects to remote device using BluetoothSocket, an exception is required.
If remote device isn't in range, It's not found
private class ConnectThread extends Thread {
public ConnectThread(BluetoothDevice device, boolean isSecure, UUID sharedUUID) throws IncorrectSetupException {
try {
//Secure connections requires to get paired before connect
//Insecure connections allows to connect without pairing
if (isSecure) {
mSocket = device.createRfcommSocketToServiceRecord(sharedUUID);
} else {
mSocket = device.createInsecureRfcommSocketToServiceRecord(sharedUUID);
}
} catch (IOException e) {
//Is there some problem with the setup?
}
}
public void run() {
try {
mSocket.connect();
} catch (IOException e) {
//If device is not found, this exception is throwed
}
}
}
I am developing an app for bluetooth remote control a small robot (no, not arduino). The robot has a bluetooth chip (BK3221), which, from what I've gotten to know through their UUIDs works with A2DP and AVRCP protocols (oriented audio).
UUID 1: 0000110b-00000-1000-8000-00805f9b34fb
UUID 2: 0000110e-00000-1000-8000-00805f9b34fb
I can create the connection of the mobile and device through the connect() of Bluetooth A2DP classbut that is what I have, I have the proxy and the connected device but I do not know how to give information .
Moreover I tried program the connection in a basic way with functions for android with RFCOMM (which is supposed to support AVRCP). The robot is a closed system and I don´t know if it worked as a server or client (I assumed it was because the server because it accepts the conexion with the function "conect()" in the previous case). But when I call the connect function generates an exception BluetoothSocket me: "JSR82 connect connection is not created (failed or aborted)". I looked on JSR82 but gave me the feeling of being obsolete ...
If anyone has any idea of something... Thanks
Code of the connection with BluetoothA2dp:
//adaptador == BluetoothAdapter and proxy1 == BluetoothA2dp
protected Boolean doInBackground(BluetoothDevice device) throws RemoteException {
Method connect = getConnectMethod();
final BluetoothProfile.ServiceListener mProfileListener = new BluetoothProfile.ServiceListener() {
public void onServiceConnected(int profile, BluetoothProfile proxy) {
if (profile == BluetoothProfile.A2DP) {
proxy1 = (BluetoothA2dp) proxy;
}
}
public void onServiceDisconnected(int profile) {
if (profile == BluetoothProfile.A2DP) {
proxy1 = null; }
}};
adaptador.getProfileProxy(getBaseContext(), mProfileListener, BluetoothProfile.A2DP);
try {
connect.setAccessible(true);
connect.invoke(proxy1, device);
Toast.makeText(getBaseContext(),"Connection OK!", Toast.LENGTH_LONG).show();
} catch (Exception ex) {
Toast.makeText(getBaseContext(),"Connection Error"+ex.getMessage(), Toast.LENGTH_LONG).show();
}
private Method getConnectMethod () {
try {
return BluetoothA2dp.class.getDeclaredMethod("connect", BluetoothDevice.class);
} catch (NoSuchMethodException ex) {
Toast.makeText(getBaseContext(),"Method dont appear", Toast.LENGTH_LONG).show();
return null;
}
}
I am trying to establish Bluetooth connection between an Android device with other mobile phone over Handsfree profile. I am using following code -
private static final UUID MY_UUID = UUID.fromString("0000111F-0000-1000-8000-00805F9B34FB"); // UUID for Hands free profile
// Some code...
// Get Bluetooth Adapter.
m_oBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
// Some code...
// For paired BT device, getting a connection established.
if(null != m_oBluetoothDevice)
{
if(BluetoothDevice.BOND_BONDED == m_oBluetoothDevice.getBondState())
{
try
{
m_oBluetoothSocket = m_oBluetoothDevice.createRfcommSocketToServiceRecord(MY_UUID);
m_oBluetoothSocket.connect();
Log.i(TAG, "Socket Connected");
}
catch(Exception e)
{
if(null != m_oBluetoothSocket)
{
Log.i(TAG, "Closing socket");
try
{
m_oBluetoothSocket.close();
}
catch (Exception e1)
{
Log.i(TAG, "Error while closing socket : " + e1.getMessage());
}
}
}
}
}
I can create RFCOMMSocket using this code.
Now I want to send AT commands based on Bluetooth Hands-Free profile. e.g. If other mobile phone receives a phone call, my Android device can reject this call by sending AT command- "+CHUP". I am not sure whether this is possible or not.
At this point, I am stuck. I have read Bluetooth APIs where I found -
BluetoothHeadset.ACTION_VENDOR_SPECIFIC_HEADSET_EVENT
Can we use this Intent for sending AT commands? Is this a proper way to send AT command based on Bluetooth Hands-Free profile? Please someone help me out and give me proper direction.
Any input from you all will be great help for me.
Thanks in advance.
You need to create InputStream and OutputStream so you can talk to the phone:
mmInStream = m_oBluetoothSocket.getInputStream();
mmOutStream = m_oBluetoothSocket.getOutputStream();
To setup the HFP connection you start to send:
mmOutStream.write("AT+BRSF=20\r".getBytes());
Where 20 is code for what you support of HFP.
And to read from the phone:
buffer = new byte[200];
mmInStream.read(buffer);
command = new String(buffer).trim();
So now you can talk beetwen the devices and you can read more about the Handsfree profile on https://www.bluetooth.org/docman/handlers/downloaddoc.ashx?doc_id=238193
Adding reference to AT commnads
http://forum.xda-developers.com/showthread.php?t=1471241
http://www.zeeman.de/wp-content/uploads/2007/09/ubinetics-at-command-set.pdf