I am building an app for my company where there is a feature it should get a heart rate from a Bluetooth watch.
I am using this library where I can get the basic data like device name and battery level and all but I can't get the data that I want (Step count and Heart rate).
Is there any other way to make it work? I have been working on this for weeks.
These are services I got from nRF:
The code that I have written:
package com.example.myapplicationrx;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.ParcelUuid;
import android.widget.Button;
import com.polidea.rxandroidble2.RxBleClient;
import com.polidea.rxandroidble2.RxBleConnection;
import com.polidea.rxandroidble2.RxBleDevice;
import java.util.UUID;
import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;
public class MainActivity extends AppCompatActivity {
Disposable connectionDisposable;
RxBleDevice device;
RxBleClient rxBleClient;
#SuppressLint("MissingPermission")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button start = findViewById(R.id.start);
Button read = findViewById(R.id.read);
Context context = getApplicationContext();
rxBleClient = RxBleClient.create(context);
String macAddress = "EE:00:52:C1:66:95";
device = rxBleClient.getBleDevice(macAddress);
start.setOnClickListener(v -> {
connectionDisposable = device.establishConnection(false)
.flatMapSingle(rxBleConnection -> rxBleConnection.readCharacteristic(convertFromInteger(0x2A37)))
.subscribe(
characteristicValue -> {
System.out.println(new String(characteristicValue));
},
throwable -> {
// Handle an error here.
System.out.println(throwable);
}
);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
connectionDisposable.dispose();
}
}, 10000);
});
}
public UUID convertFromInteger ( int i){
final long MSB = 0x0000000000001000L;
final long LSB = 0x800000805f9b34fbL;
long value = i & 0xFFFFFFFF;
return new UUID(MSB | (value << 32), LSB);
}
private Observable<RxBleConnection> prepareConnectionObservable () {
return device
.establishConnection(false);
}
private String byteArrayToHex ( byte[] a){
StringBuilder sb = new StringBuilder(a.length * 2);
for (byte b : a)
sb.append(String.format("%02x", b & 0xff));
return sb.toString();
}
}
errors when I read heart rate
I/System.out: com.polidea.rxandroidble2.exceptions.BleCharacteristicNotFoundException: Characteristic not found with UUID 00002a37-0000-1000-8000-00805f9b34fb
From the attached image, it looks like the watch doesn't have the standard Heart Rate Service (UUID 0x180D) or the standard Step Count Service (UUID 0x27BA). You can read the device name and the battery service because the Device Information Service (0x180A) and Battery Service (0x180F) exist.
If you want to be able to read the heart rate and step count, you should first understand how this data is exposed via the watch. It looks like there are two additional primary services that the watch supports, but these are not standard services, so unless there's documentation from the watch company, it would be difficult to try and read the data off these services. Your best bet would be a trial and error method where you read the data in those services and try to make sense off it knowing that they should contain heart rate and step count data.
I want to read html code of arduino web server.
I can make web server simply with from example 4. code is below.
#include <SoftwareSerial.h>
#include "WiFly.h"
#define SSID "yum2"
#define KEY "yum000000"
// check your access point's security mode, mine was WPA20-PSK
// if yours is different you'll need to change the AUTH constant, see the file WiFly.h for avalable security codes
#define AUTH WIFLY_AUTH_WPA2_PSK
int flag = 0;
// Pins' connection
// Arduino WiFly
// 2 <----> TX
// 3 <----> RX
SoftwareSerial wiflyUart(2, 3); // create a WiFi shield serial object
WiFly wifly(&wiflyUart); // pass the wifi siheld serial object to the WiFly class
void setup()
{
wiflyUart.begin(9600); // start wifi shield uart port
Serial.begin(9600); // start the arduino serial port
Serial.println("--------- WIFLY Webserver --------");
// wait for initilization of wifly
delay(1000);
wifly.reset(); // reset the shield
delay(1000);
//set WiFly params
wifly.sendCommand("set ip local 80\r"); // set the local comm port to 80
delay(100);
wifly.sendCommand("set comm remote 0\r"); // do not send a default string when a connection opens
delay(100);
wifly.sendCommand("set comm open *OPEN*\r"); // set the string that the wifi shield will output when a connection is opened
delay(100);
Serial.println("Join " SSID );
if (wifly.join(SSID, KEY, AUTH)) {
Serial.println("OK");
} else {
Serial.println("Failed");
}
delay(5000);
wifly.sendCommand("get ip\r");
char c;
while (wifly.receive((uint8_t *)&c, 1, 300) > 0) { // print the response from the get ip command
Serial.print((char)c);
}
Serial.println("Web server ready");
}
void loop()
{
if(wifly.available())
{ // the wifi shield has data available
if(wiflyUart.find("*OPEN*")) // see if the data available is from an open connection by looking for the *OPEN* string
{
Serial.println("New Browser Request!");
delay(1000); // delay enough time for the browser to complete sending its HTTP request string
// send HTTP header
wiflyUart.println("HTTP/1.1 200 OK");
wiflyUart.println("Content-Type: text/html; charset=UTF-8");
wiflyUart.println("Content-Length: 244"); // length of HTML code
wiflyUart.println("Connection: close");
wiflyUart.println();
// send webpage's HTML code
wiflyUart.print("<html>");
wiflyUart.print("<head>");
wiflyUart.print("<title>My WiFI Shield Webpage</title>");
wiflyUart.print("</head>");
wiflyUart.print("<body>");
wiflyUart.print("<h1>Hello World!</h1>");
wiflyUart.print("<h3>10 20 30 40 50</h3>");
wiflyUart.print("Yahoo! Google");
wiflyUart.print("<br/><button>My Button</button>");
wiflyUart.print("</body>");
wiflyUart.print("</html>");
}
}
}
the web server work well.
I wrote simple android code to read html is below.
package com.example.http;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
private TextView tv;
String urlAddress = "192.168.0.10"; // doesn't show
// String urlAddress = "http://www.kma.go.kr/weather/main.jsp#1159068000"; // work well
Handler handler = new Handler();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.textView1);
Button b = (Button)findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
loadHtml();
}
});
}
void loadHtml() {
Thread t = new Thread(new Runnable() {
#Override
public void run() {
final StringBuffer sb = new StringBuffer();
try {
URL url = new URL(urlAddress);
HttpURLConnection conn =
(HttpURLConnection) url.openConnection();
if (conn != null) {
conn.setConnectTimeout(2000);
conn.setUseCaches(false);
if (conn.getResponseCode()
== HttpURLConnection.HTTP_OK) {
BufferedReader br
= new BufferedReader(new InputStreamReader
(conn.getInputStream()));
while (true) {
String line = br.readLine();
if (line == null) break;
sb.append(line + "\n");
}
br.close();
}
conn.disconnect();
}
Log.d("test", sb.toString());
handler.post(new Runnable() {
#Override
public void run() {
tv.setText(sb.toString());
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
});
t.start();
}
}
It work very well with String urlAddress = "http://www.kma.go.kr/weather/main.jsp#1159068000";
but it doesn't show when using
String urlAddress = "192.168.0.10"; instead of before one.
the 192.168.0.10 is allocated ip of arduino from DHCP that i can check on serial monitor.
there is any way to read html from arduino web server??
Your urlAddress isn't a URL, it's just an IP address. You should fully qualify the address with the protocol and the path you want to land on, for example:
String urlAddress = "http://192.168.0.10/";
I would expect you're getting a MalformedURLException.
I am trying to find a way to have a tablet basically auto-accept/give permission to accept an incoming Bluetooth Share file transferred from a laptop to my Nexus Android device. Since the Android bluetooth system itself does not support this feature, I was wondering if this could be done programmatically using an Android application that listened for the notification and accepted it for me.
MUCH EASIER WAY
If you have a rooted device and use XPosed Framework, your goal can be achieved much easier.
You Need not implement your own bluetooth server nor kill the original BT service, which are very bothering!!!
xposed tutorial link.
Try this code.
import android.util.*;
import de.robv.android.xposed.*;
import de.robv.android.xposed.callbacks.XC_LoadPackage.*;
import static de.robv.android.xposed.XposedHelpers.findAndHookMethod;
public class Tutorial implements IXposedHookLoadPackage
{
private String TAG="TUTORIAL";
public void handleLoadPackage(final LoadPackageParam lpparam) throws Throwable {
if (!lpparam.packageName.equals("com.android.bluetooth"))
{
Log.i(TAG,"Not: "+lpparam.packageName);
return;
}
Log.i(TAG,"Yes "+lpparam.packageName);
findAndHookMethod("com.android.bluetooth.opp.BluetoothOppManager", lpparam.classLoader, "isWhitelisted", String.class,new XC_MethodHook() {
#Override
protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
Log.v(TAG,"HOOK DONE");
param.setResult(true); /* you can compare the sender address(String) with your computer and determine if you return true or just allow the original method to be called after this returns.*/
}
});
}
}
I tested and it works fine:)
Links
Dropbox link of the auto accepting app
Dropbox link of the project files (zip)
Xposed apk site
Towelroot site to root your phone
Background(Original answer)
As I commented above, you bay be able to, and I tried and succeeded in blocking (though not receiving) with this code.
import android.util.*;
import de.robv.android.xposed.*;
import de.robv.android.xposed.callbacks.XC_LoadPackage.*;
import java.io.*;
import static de.robv.android.xposed.XposedHelpers.findAndHookMethod;
public class Tutorial implements IXposedHookLoadPackage
{
private String TAG="TUTORIAL";
public void handleLoadPackage(final LoadPackageParam lpparam) throws Throwable {
if (!lpparam.packageName.equals("com.android.bluetooth"))
{
Log.i(TAG,"Not: "+lpparam.packageName);
return;
}
Log.i(TAG,"Yes "+lpparam.packageName);
findAndHookMethod("com.android.bluetooth.opp.BluetoothOppService", lpparam.classLoader, "startSocketListener", new XC_MethodHook() {
#Override
protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
Log.v(TAG,"HOOK DONE");
param.setResult(null);
}
});
}
}
The code above hooks the method startListenerSocket() of com.android.bluetooth.BluetoothOppService and prevents the original method from being called by the line param.setResult(null);
Refer to here to see the full code of com.android.bluetooth.BluetoothOppService.java and you will understand the operation.
And the code you can start from is shown below.
import android.util.*;
import de.robv.android.xposed.*;
import de.robv.android.xposed.callbacks.XC_LoadPackage.*;
import static de.robv.android.xposed.XposedHelpers.findAndHookMethod;
public class Tutorial implements IXposedHookLoadPackage
{
private String TAG="TUTORIAL";
public void handleLoadPackage(final LoadPackageParam lpparam) throws Throwable {
if (!lpparam.packageName.equals("com.android.bluetooth"))
{
Log.i(TAG,"Not: "+lpparam.packageName);
return;
}
Log.i(TAG,"Yes "+lpparam.packageName);
findAndHookMethod("com.android.bluetooth.opp.BluetoothOppObexServerSession", lpparam.classLoader, "onPut", new XC_MethodHook() {
#Override
protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
Log.v(TAG,"HOOK DONE");
Class c=param.thisObject.getClass();
}
});
}
}
This code hooks the onPut method of com.android.bluetooth. BluetoothOppObexServerSession linked here. I either am newbie to xposed framework but I hope my answer helped.
I had the same issues you asked and partially solved the problem by implementing my custom OBEX server and manually / programmatically(with ps|grep and su kill pid) killing the native BluetoothOppService. But I will either try the idea of hooking and directly executing my code.
And to help you customize OBEX server session I post my implementation below.
#Override
public int onPut(Operation op)
{
if (D)
{
Log.d(TAG, "onPut " + op.toString());
}
HeaderSet request;
String name, mimeType;
Long length;
String extension=null;// type;
int obexResponse = ResponseCodes.OBEX_HTTP_OK;
String destination;
if (mTransport instanceof BluetoothObexTransport)
{
destination = ((BluetoothObexTransport) mTransport).getRemoteAddress();
}
else
{
destination = "FF:FF:FF:00:00:00";
}
boolean isWhitelisted =IsWhitelisted(destination);
try
{
boolean preReject = false;
request = op.getReceivedHeader();
if (V)
{
// Constants.logHeader(request);
}
name = (String) request.getHeader(HeaderSet.NAME);
length = (Long) request.getHeader(HeaderSet.LENGTH);
mimeType = (String) request.getHeader(HeaderSet.TYPE);
if (length == 0)
{
if (D)
{
Log.w(TAG, "length is 0, reject the transfer");
}
preReject = true;
obexResponse = ResponseCodes.OBEX_HTTP_LENGTH_REQUIRED;
}
if (name == null || name.isEmpty())
{
if (D)
{
Log.w(TAG, "name is null or empty, reject the transfer");
}
preReject = true;
obexResponse = ResponseCodes.OBEX_HTTP_BAD_REQUEST;
}
int dotIndex = name.lastIndexOf(".");
if (dotIndex > 0)
{
extension = name.substring(dotIndex + 1).toLowerCase();
}
// Reject policy: anything outside the "white list" plus unspecified
// MIME Types. Also reject everything in the "black list".
// if (!preReject && (mimeType == null || (!isWhitelisted && !Constants.mimeTypeMatches(
// mimeType, Constants.ACCEPTABLE_SHARE_INBOUND_TYPES))
// || Constants.mimeTypeMatches(mimeType,
// Constants.UNACCEPTABLE_SHARE_INBOUND_TYPES))) {
// if (D) {
// Log.w(TAG, "mimeType is null or in unacceptable list, reject the transfer");
// }
// preReject = true;
// obexResponse = ResponseCodes.OBEX_HTTP_UNSUPPORTED_TYPE;
// }
if (preReject && obexResponse != ResponseCodes.OBEX_HTTP_OK)
{
// some bad implemented client won't send disconnect
return obexResponse;
}
}
catch (IOException e)
{
Log.e(TAG, "get getReceivedHeaders error " + e);
return ResponseCodes.OBEX_HTTP_BAD_REQUEST;
}
int status = receiveFile(destination, name, extension, length, op);
/*
* TODO map status to obex response code
*/
if (status != BluetoothShare.STATUS_SUCCESS)
{
obexResponse = ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
}
Log.d(TAG, "MIME TYPE)" + mimeType);
return obexResponse;
}
I just removed some rejecting codes from the original one.
Also to look at my full code please refer to my git repository.
I also thank the contributors to the android project!
I am uploading the code of both the android-studio as well as the arduino uno.. It seems right but somehow it doesn't work
Android Studio code :
package com.example.chintan.doorlock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Set;
import java.util.UUID;
import android.os.Handler;
public class MainActivity extends AppCompatActivity {
private final String DEVICE_ADDRESS = "00:21:13:01:ED:10"; //MAC Address of Bluetooth Module
private final UUID PORT_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
private BluetoothDevice device;
private BluetoothSocket socket;
private OutputStream outputStream;
private InputStream inputStream;
Thread thread;
byte buffer[];
boolean stopThread;
boolean connected = false;
String command;
Button lock_state_btn, bluetooth_connect_btn;
TextView lock_state_text;
ImageView lock_state_img;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lock_state_btn = (Button) findViewById(R.id.lock_state_btn);
bluetooth_connect_btn = (Button) findViewById(R.id.bluetooth_connect_btn);
lock_state_text = (TextView) findViewById(R.id.lock_state_text);
lock_state_img = (ImageView) findViewById(R.id.lock_state_img);
bluetooth_connect_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
if(BTinit()) //initialises bluetooth
{
BTconnect(); //connects android code to arduino
beginListenForData();
// The code below sends the number 3 to the Arduino asking it to send the current state of the door lock so the lock state icon can be updated accordingly
command = "3";
try
{
outputStream.write(command.getBytes());
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
});
lock_state_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
if(connected == false)
{
Toast.makeText(getApplicationContext(), "Please establish a connection with the bluetooth servo door lock first", Toast.LENGTH_SHORT).show();
}
else
{
command = "1";
try
{
outputStream.write(command.getBytes()); // Sends the number 1 to the Arduino. For a detailed look at how the resulting command is handled, please see the Arduino Source Code
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
});
}
void beginListenForData() // begins listening for any incoming data from the Arduino
{
final Handler handler = new Handler();
stopThread = false;
buffer = new byte[1024];
Thread thread = new Thread(new Runnable()
{
public void run()
{
while(!Thread.currentThread().isInterrupted() && !stopThread)
{
try
{
int byteCount = inputStream.available();
if(byteCount > 0)
{
byte[] rawBytes = new byte[byteCount];
inputStream.read(rawBytes);
final String string = new String(rawBytes, "UTF-8");
handler.post(new Runnable()
{
public void run()
{
if(string.equals("3"))
{
lock_state_text.setText("Lock State: LOCKED"); // Changes the lock state text
lock_state_img.setImageResource(R.drawable.locked_icon); //Changes the lock state icon
}
else if(string.equals("4"))
{
lock_state_text.setText("Lock State: UNLOCKED");
lock_state_img.setImageResource(R.drawable.unlocked_icon);
}
}
});
}
}
catch (IOException ex)
{
stopThread = true;
}
}
}
});
thread.start();
}
#Override
protected void onStart()
{
super.onStart();
}
}
And this is my arduino source code :
//Android-controlled Arduino Bluetooth Servo Door Lock by uscv
#include <Servo.h>
#include <EEPROM.h>
Servo servo;
char state;
void setup() {
// put your setup code here, to run once:
servo.attach(7);
if(EEPROM.read(0) == 1) // Reads the EEPROM value stored to know what state the door lock was in before it was last turned off
{ // An EEPROM value of 1 means UNLOCKED and a value of 2 means LOCKED
servo.write(0); // Rotates the servo to the unlocked position
delay(200);
}
else if(EEPROM.read(0) == 2)
{
servo.write(75); // Rotates the servo to the locked position
delay(200);
}
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
if(Serial.available() > 0)
{
char data;
data = Serial.read(); // The variable data is used to store the value sent by the Android app
switch(data)
{
case '1':
if(EEPROM.read(0) == 1) //An EEPROM value of 1 means it is currently unlocked
{
EEPROM.write(0, 2); // Writes the number 2 to address 0 on the Arduino's EEPROM. This value will be used by the Arduino to remember the last state the door lock was in
Serial.print("3"); // Sends the number 3 to the Android app. To see what this does, please see the Android Studio Project file
servo.write(75);
delay(15);
}
else if(EEPROM.read(0) == 2) //An EEPROM value of 2 means it i currently locked
{
EEPROM.write(0, 1); // Writes the number 1 to address 0 on the Arduino's EEPROM. This value will be used by the Arduino to remember the last state the door lock was in
Serial.print("4"); // Sends the number 4 to the Android app. The number sent will be used by the app to update the locked/unlocked icon
servo.write(0);
delay(15);
}
break;
case '3':
if(EEPROM.read(0) == '1')
{
Serial.print("4");
}
else if(EEPROM.read(0) == '2')
{
Serial.print("3");
}
break;
}
}
}
So, first the android app will start bluetooth, then connect to the arduino uno card and then perform the following functions:
1. "3" sent from android phone to arduino
2. received either "4" or "3" based on its status from arduino uno to android
3. Change the lock image on the android phone
4. send "1" from the android phone to the arduino
5. Change the status of the servo motor sg90 from its current position
Now, my android application is error-free and connects to the arduino uno and also is able to start the bluetooth of the phone. It establishes the connection but does not change the position of the servo motor sg90. I dont understand the problem what should we do?
You are calling EEPROM.read() before initializating the EEPROM. In other words for example in your setup() you are checking whether you EEPROM address 0 contains a certain value rather than initialization it with a certain value. Then
in your loop(), again you are checking whether EEPROM address contains a certain value before any value is written to the EEPROM and thus you will never reach the EEPROM write part because your if statements always returns false. So, start first by writing a value to EEPROM address 0.
I have problem working on an Android accessory job with Arduino Due which is uploaded Blink.ino (other samples too). The problem is that I couldn't detect any USB accessory (or List) from UsbManager got from getSystemService("USB_SERVICE") in any case with attaching or detaching.
My code is below.
1. manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.signalhello"
android:versionCode="1"
android:versionName="1.0" >
<!-- <uses-feature android:name="android.hardware.usb.accessory" />
<uses-feature android:name="android.hardware.usb.host" /> -->
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.signalhello.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" />
<action android:name="android.hardware.usb.action.USB_ACCESSORY_DETACHED" />
</intent-filter>
<meta-data
android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED"
android:resource="#xml/accessory_filter" />
</activity>
</application>
</manifest>
2. accessory-filter.xml in res/xml/
<?xml version="1.0" encoding="utf-8"?>
<resources>
<usb-accessory manufacturer="Arduino-er"/> // I tried other options like <usb- accessory vendor-id="9025" product-id="62"/>
<usb-device vendor-id="9025" product-id="62"/>
<usb-accessory vendor-id="2341" product-id="003e"/>
<usb-device vendor-id="2341" product-id="003e"/>
</resources>
3. MainActivity
package com.example.signalhello;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbAccessory;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
#TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
public class MainActivity extends Activity {
private static final String ACTION_USB_PERMISSION =
"net.hardroid.adk.example.action.USB_PERMISSION";
private PendingIntent mPermissionIntent;
private boolean mPermissionRequestPending;
// USB가 감지되었을 때의 이벤트를 받음.
private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.d("onReceive", "action: " + action);
tv01.setText("action: " + action);
if (ACTION_USB_PERMISSION.equals(action)) {
// 사용자에게 Android Accessory Protocol을 구현한 장비가 연결되면
// 수락할 것인지 문의한 다이얼로그에 대한 사용자의 선택 결과를 받는다.
synchronized (this) {
Log.d("onReceive", "Getting UsbAccessory...");
UsbAccessory accessory = intent
.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
UsbDevice usbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
Log.d("onReceive", "Accessory is null ? " + (accessory == null) + ", Device is null ? " + (usbDevice == null));
if (intent.getBooleanExtra(
UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
Log.d("onReceive", "Connected USB ...");
// 수락했을 경우
showMessage("receiver : USB Host 연결됨.");
}
else {
Log.d("onReceive", "Permission denied....");
Log.d(MainActivity.class.getName(),
"permission denied for accessory " + accessory);
showMessage("permission denied.");
}
openAccessory(accessory);
UsbManager manager = null;
manager = (UsbManager) getSystemService(Context.USB_SERVICE);
HashMap<String, UsbDevice> deviceList = manager
.getDeviceList();
Iterator<UsbDevice> deviceIter = deviceList.values()
.iterator();
while (deviceIter.hasNext()) {
UsbDevice device = deviceIter.next();
// Device's class...
int count = device.getInterfaceCount();
tv01.setText("count:" + count);
for (int i = 0; i < count; i++) {
UsbInterface iface = device.getInterface(i);
tv02.setText(tv02.getText() + "," + iface.getId());
}
}
// 연결 수락 결과를 받았음을 표시
mPermissionRequestPending = false;
tv04.setText("3");
}
}
else
if (UsbManager.ACTION_USB_ACCESSORY_DETACHED.equals(action)) {
// Android Accessory Protocol을 구현한 장비의 연결이 해제되었을 때
UsbAccessory accessory = intent
.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
// 앱이 사용하고 있는 장비와 같은 것인지 확인
if (accessory != null && accessory.equals(mAccessory)) {
showMessage("USB Host 연결 해제됨.");
closeAccessory();
}
tv04.setText("4");
}
tv04.setText("5");
}
};
private TextView tv01;
private TextView tv02;
private TextView tv03;
private TextView tv04;
/*static class IncomingHandler extends Handler {
private final WeakReference<UDPListenerService> mService;
IncomingHandler(UDPListenerService service) {
mService = new WeakReference<UDPListenerService>(service);
}
#Override
public void handleMessage(Message msg)
{
UDPListenerService service = mService.get();
if (service != null) {
service.handleMessage(msg);
}
}
}*/
/*Handler mIncomingHandler = new Handler(new Handler.Callback() {
#Override
public boolean handleMessage(Message msg) {
}
});*/
static Handler uiHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
Log.d("handleMessage", "msg.what: " + msg.what + ", msg.obj: " + msg.obj);
switch (msg.what) {
case 1:
}
}
};
private UsbAccessory mAccessory;
private AdkHandler handler;
private static UsbManager mUsbManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv01 = (TextView) findViewById(R.id.tv01);
tv02 = (TextView) findViewById(R.id.tv02);
tv03 = (TextView) findViewById(R.id.tv03);
tv04 = (TextView) findViewById(R.id.tv04);
mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
Log.d("onCreate", "Permission intent getting...");
mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(
ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
filter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED);
filter.addAction(UsbManager.ACTION_USB_ACCESSORY_ATTACHED);
Log.d("onCreate", "registering Receiver..");
Log.d("onCreate", "receiver is null ? " + (mUsbReceiver == null));
registerReceiver(mUsbReceiver, filter);
Log.d("onCreate", "registered Receiver..");
Log.d("onCreate", "Got permission intent...");
tv04.setText("1");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/** 액티비티가 화면에 보일 때 호출 */
#Override
public void onResume() {
super.onResume();
Log.d("onResume", "Getting device list");
// 앱이 화면에 보일 때 안드로이드 장비에 Android Accessory Protocol을
// 구현한 USB Host가 연결되어 있는지 확인
HashMap<String, UsbDevice> devices = mUsbManager.getDeviceList();
Log.d("onResume", "Getting UsbAccessory");
UsbAccessory accessory = getIntent().getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
if (accessory != null) { // Android Accessory Protocol를 구현한 장비를 찾았을 경우
Log.d("onResume", "Got accessory");
if (mUsbManager.hasPermission(accessory)) {
Log.d("onResume", "Got permission");
showMessage("onresume : USB Accessory 연결됨.");
openAccessory(accessory);
}
else {
synchronized (mUsbReceiver) {
if (!mPermissionRequestPending) {
mUsbManager.requestPermission(accessory,
mPermissionIntent); // USB 연결을 통해 장비에 연결해도 되는지 사용자에게 문의
mPermissionRequestPending = true; // 연결권한을 물어보드 코드를 실행했음을 표시
}
}
}
}
else {
String device = "";
if (devices != null) {
Set<String> keys = devices.keySet();
for (String key : keys) {
device += key + ": " + devices.get(key).getDeviceName();
}
}
showMessage("mAccessory is null, devices: " + (devices == null ? "null" : device));
Log.d(MainActivity.class.getName(), "mAccessory is null");
}
tv04.setText("2");
}
// 액티비티가 소멸될 때 호출
#Override
protected void onDestroy() {
// 브로드캐스트 리시버를 제거
unregisterReceiver(mUsbReceiver);
super.onDestroy();
}
private void showMessage(String msg){
Log.d("showMessage", msg);
tv03.setText("message: " + msg);
}
private void openAccessory(UsbAccessory accessory){
mAccessory = accessory;
if(handler == null){
Log.d("openAccessory", "Instanciating AdkHandler");
handler = new AdkHandler();
Log.d("openAccessory", "Setting UI Handler to AdkHandler");
handler.setUiHandler(uiHandler);
}
Log.d("openAccessory", "Openning AdkHandler");
handler.open(mUsbManager, mAccessory);
}
private void closeAccessory(){
if(handler != null && handler.isConnected())
handler.close();
mAccessory = null;
}
}
3-1.AdkHandler.java
package com.example.signalhello;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import android.annotation.TargetApi;
import android.hardware.usb.UsbAccessory;
import android.hardware.usb.UsbManager;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.os.ParcelFileDescriptor;
import android.util.Log;
/**
*
* #author Chun, Young-yil. at IBS Inc.
* #since ADK 4.0
* #date 2013. 5. 15.
*/
#TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
public class AdkHandler implements Runnable {
private ParcelFileDescriptor mFileDescriptor;
private FileInputStream mInputStream;
private FileOutputStream mOutputStream;
private Handler uiHandler;
private boolean running;
/* (non-Javadoc)
* #see java.lang.Runnable#run()
*/
#Override
public void run() {
Log.d("run", "start");
int ret = 0;
byte[] buffer = new byte[16384];
int i;
while (ret >= 0 && running) {
try {
ret = mInputStream.read(buffer);
Log.d("run", "" + ret);
}
catch (Exception e) {
e.printStackTrace();
break;
}
i = 0;
Log.d("run", "i: " + i + ", ret: " + ret);
while ( i < ret && running) {
int len = ret - i;
Log.d("run", "buffer[" + i + "]: " + buffer[i]);
switch (buffer[i]) {
case 0x1:
if (len >= 3) {
Message m = Message.obtain(uiHandler, 1);
int value = composeInt(buffer[i = 1], buffer[i + 2]);
m.obj = value;
uiHandler.sendMessage(m);
Log.d("value", "" + value);
}
i += 3;
break;
default :
Log.d("default", "unknown msg:" + buffer[i]);
i = len;
break;
}
}
}
}
/**
* #param b
* #param c
* #return
*/
private int composeInt(byte high, byte low) {
int val = (int) high & 0xff;
val *= 256;
val += (int) low & 0xff;
return val;
}
public void open(UsbManager usbManager, UsbAccessory accessory) {
Log.d("open", "Getting FileDescriptor");
mFileDescriptor = usbManager.openAccessory(accessory);
if (mFileDescriptor == null) {
Log.d("open", "Failed to get ParcelFileDescriptor");
Log.d("보드연결", "실패");
Message msg = Message.obtain(uiHandler, -1);
msg.obj = "보드연결 실패";
Log.d("open", "Sending fail message to UI Handler...");
uiHandler.sendMessage(msg);
}
else
{
Log.d("open", "Succeeded in getting ParcelFileDescriptor");
Log.d("open", "Getting FileDescriptor from ParcelFileDescriptor");
FileDescriptor fd = mFileDescriptor.getFileDescriptor();
Log.d("open", "Getting FileInputStream...");
mInputStream = new FileInputStream(fd);
Log.d("open", "New Threading..");
Thread thread = new Thread(null, this, "ADK Example");
running = true;
Log.d("open", "Starting thread...");
thread.start();
Log.d("보드연결", "성공");
}
}
public void close() {
Log.d("close", "Closing...");
running = false;
}
/**
* 멤버 uiHandler 을 회수
* #return the uiHandler
*/
public Handler getUiHandler() {
return uiHandler;
}
/**
* sets the uiHandler of this instance to given uiHandler.
* 멤버속성 uiHandler 을 인수 uiHandler 으로 세팅.
* #param uiHandler the uiHandler to set
*/
public void setUiHandler(Handler uiHandler) {
this.uiHandler = uiHandler;
}
/**
* #return
*/
public boolean isConnected() {
return (mInputStream != null && mOutputStream != null);
}
}
_4. acivity_main.xml_
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:id="#+id/ll01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="vertical" >
<TextView
android:id="#+id/tv01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="장비미인식" />
<TextView
android:id="#+id/tv02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-1" />
<TextView
android:id="#+id/tv03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-1" />
<TextView
android:id="#+id/tv04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-1" />
</LinearLayout>
</RelativeLayout>
5. logcat ( using adbwireless in rooting MPgio minipad )
05-31 12:16:12.728: I/wpa_supplicant(400): [CTRL_IFACE]SIGNAL_POLL
05-31 12:16:15.298: W/PowerUI(207): unknown intent: Intent { act=android.intent.action.ACTION_POWER_CONNECTED flg=0x10000010 }
05-31 12:16:15.488: I/WindowManager(140): MediaPlayer.is not PlayingVideo
05-31 12:16:15.728: I/wpa_supplicant(400): [CTRL_IFACE]SIGNAL_POLL
05-31 12:16:16.088: I/WindowManager(140): MediaPlayer.is not PlayingVideo
05-31 12:16:17.218: I/USB3G(86): event { 'add', '/devices/platform/sw_hcd_host0/usb1/1-1', 'usb', '', 189, 15 }
05-31 12:16:17.218: I/USB3G(86): path : '/sys/devices/platform/sw_hcd_host0/usb1/1-1'
05-31 12:16:17.218: I/USB3G(86): VID :size 5,vid_path '/sys/devices/platform/sw_hcd_host0/usb1/1-1/idVendor',VID '2341
05-31 12:16:17.218: I/USB3G(86): '.
05-31 12:16:17.218: I/USB3G(86): PID :size 5,Pid_path '/sys/devices/platform/sw_hcd_host0/usb1/1-1/idProduct',PID '003e
05-31 12:16:17.218: I/USB3G(86): '.
05-31 12:16:17.218: I/USB3G(86): cmd=/system/etc/usb_modeswitch.sh /system/etc/usb_modeswitch.d/2341_003e &,
05-31 12:16:17.228: E/EventHub(140): could not get driver version for /dev/input/mouse0, Not a typewriter
05-31 12:16:17.228: D/EventHub(140): No input device configuration file found for device 'Arduino LLC Arduino Due '.
05-31 12:16:17.248: I/EventHub(140): New device: id=15, fd=247, path='/dev/input/event4', name='Arduino LLC Arduino Due ', classes=0x8000000b, configuration='', keyLayout='/system/usr/keylayout/Generic.kl', keyCharacterMap='/system/usr/keychars/Generic.kcm', builtinKeyboard=false
05-31 12:16:17.298: I/USB3G(86): excute ret : 0,err:No such file or directory
05-31 12:16:17.318: I/InputReader(140): Device added: id=15, name='Arduino LLC Arduino Due ', sources=0x00002103
05-31 12:16:17.318: I/ActivityManager(140): Config changed: {1.3 0mcc0mnc ko_KR layoutdir=0 sw640dp w1066dp h592dp lrg land finger qwerty/v/v -nav/h s.33}
05-31 12:16:17.338: D/OpenGLRenderer(1631): Flushing caches (mode 0)
05-31 12:16:17.478: D/Activity(1631): pckname = com.example.signalhello mComponent = com.example.signalhello.MainActivity
05-31 12:16:17.538: I/StatusBar.HeightReceiver(207): Resizing status bar plugged=false height=36 old=36
05-31 12:16:17.658: D/onCreate(1631): Permission intent getting...
05-31 12:16:17.658: D/onCreate(1631): registering Receiver..
05-31 12:16:17.668: D/onCreate(1631): receiver is null ? false
05-31 12:16:17.668: D/onCreate(1631): registered Receiver..
05-31 12:16:17.668: D/onCreate(1631): Got permission intent...
05-31 12:16:17.698: D/onResume(1631): Getting device list
05-31 12:16:17.698: D/onResume(1631): Getting UsbAccessory
05-31 12:16:17.698: D/showMessage(1631): mAccessory is null, devices:
05-31 12:16:17.698: D/com.example.signalhello.MainActivity(1631): mAccessory is null
05-31 12:16:17.728: D/ViewRootImpl(1631): pckname = com.example.signalhello
05-31 12:16:18.138: I/WindowManager(140): MediaPlayer.is not PlayingVideo
05-31 12:16:18.738: I/wpa_supplicant(400): [CTRL_IFACE]SIGNAL_POLL
05-31 12:16:20.068: D/dalvikvm(140): GC_EXPLICIT freed 267K, 24% free 8736K/11399K, paused 8ms+13ms
05-31 12:16:21.738: I/wpa_supplicant(400): [CTRL_IFACE]SIGNAL_POLL
I'm very confused with my situation with above things.
The Arduino board is connected with my notebook PC (programming port). (I think it provides power to the board, doesn't it?) Because the power supply is needed, no other power supplier I have.
And, I compiled Blink.ino which I downloaded from Arduino's site. (I have tested other sketches. But no news.)
And, the board works blinking when uploaded by the Arduino code writing tool.
But when I attached (connected) the board by USB cable to Android device MPgio MiniPad, I have no sign of detection with my codes above.
Just system_process issues some strange messages (which are included above logcat messages) like '05-31 12:16:17.228: E/EventHub(140): could not get driver version for /dev/input/mouse0, Not a typewriter' or '05-31 12:16:17.228: D/EventHub(140): No input device configuration file found for device 'Arduino LLC Arduino Due '.
But, just below those, it prints 'New device: id......' which is from system_process's EventHub Tag.
I want to just detect my Arduino board.
I have searched to solve this problem many sites, blogs etc. And finally I became to have a doubt about that the hardware - MPgio - support this kind of application. Because I found some blogs say all kinds of android device necessarily do not support this. So I called to MPgio customer center to let me contact with technical supervisor or consultant who could answer whether the device supports the ADK - USB Accessory, Host application.
Having talked with another man, I was told to get a call later by another man who has an answer for that.
And I was called when my way to home. And he had asked some questions about my requests, but he couldn't answer by right that time, instead he promised to call me again with answer for this. And finally! I got a call of who made a promise, and he said the device does not support this. And it was kernel problem.
And he added they have a case to install the driver -FTDI? I heard- for distribution of which meets specific client requirements (with payment included, I heard).
... And I am heard that if I need that I must pay or rent - 'lease' I may resume the way he said - this on which is replied that I am not the man who could answer this right the time...
That's all.
But I might have to add some for other developers who might have problems similar to this.
First of all, the Blink.ino is not the good case which has not the code of declaration of ADK instance that might be the protocol which can interface to other supporting device.
Actually, I have tested other codes which have such an announcement. But it didn't make a success anyway - in my case, the device not supported.
And, I have to write a minute thing that the arduino 1.5.2 uses ADK library which replaced
the AndroidAccessory(.h) library being used in earlier version - whiche version I cannot point out specifically.
In some blogs I followed the way of making codes, and imported the AndroidAccessory library , compiled to failure. So I went further, and another blogs I found the case 1.5.2 uses the ADK - the site is
'http://arduino-er.blogspot.kr/2013/03/hello-world-adk-communication-between.html'.
Actually this sites is the final case I am referencing for my hello codes now.
But that sure failed to work in success - in my case, the device problem I mentioned.
If other things happen about this which have to be posted, I will write those again.
Thank you all who has made an effort to solve this anyway...
After my first own answering, I had to know about the devices which might be compatible with ADK 2012, I asked about that to Arduino google group -https://groups.google.com/a/arduino.cc/group/developers/browse_thread/thread/955aa9342270c2a2/8e03022da7aeab3f#8e03022da7aeab3f - and to another Korean site named 'Hardroid' -
http://hardroid.net, and was replied with messages
which he wasn't sure that he could name the compatible devices
specifically.
But he was much sure that the reference phones and tablets from Google
Nexus series would compatible with ADK which might be adopted in
Arduino Due I'm using.
The result is this. We, my company has decided to change the model of tablet, and the board. What surprised me is that the ADK.h in newer version of Arduino board does not work with new tablet working on Jelly Bean (Android SDK 4.2.2) while AndroidAccessory.h does work.
I almost omitted the fact we changed the board from Due to Mega_ADK.
I heard that the Due does not work with ADK properly from my colleage who contacted the vendor in Korea, but actually I can't believe this because I had seen the U-tube file working Due with
device I don't remember the model (probably HTC's).
I'd finished USB-Accessory job with newer tablet ASUS Memo-pad with success.
I don't know the exact reason of not working with ADK.h in sketch right now.