步骤1:创建新的Android项目
打开Eclipse,打开File-》 New-》 Android Application Project ,然后在“应用程序名称”编辑框中填写应用程序名称,例如BleExample或其他。最低必需的SDK选择API18:Android 4.3,并且目标SDK也选择API18:Android 4.3,因为buletooth 4.0必须具有Android 4.3版本或更高版本。其他默认保持不变,请继续单击“下一步”按钮,直到出现“完成”按钮,然后单击“完成”按钮。
步骤2:添加权限和服务
在清单文件中添加以下代码:
步骤3:创建ListView项目布局文件
旨在显示ListView的每个内容,此处我们使用自定义(自己定义),以便每个ListView可以显示更多内容,item_list.xml如下所示:
将BleExample/com.elecfreaks.ble的源代码复制到您的项目src目录中,然后在出现错误提示的情况下按Shift + Ctrl + O键打开文件。
步骤4:修改Activity_main.xml,增加ScanButton和BleDeviceListView
增加的内容如下所示:
android:id=“@+id/scanButton”
android:layout_width=“fill_parent”
android:layout_height=“wrap_content”
android:onClick=“scanOnClick”
android:text=“scan” /》
android:id=“@+id/bleDeviceListView”
android:layout_width=“fill_parent”
android:layout_height=“wrap_content”
android:layout_alignLeft=“@+id/scanButton”
android:layout_below=“@+id/scanButton”
android:layout_above=“@+id/sendButton”
》
步骤5:在MainActivity.java中,添加响应事件的ScanButton方法
(onClick=“scanOnClick”)
public void scanOnClick(final View v){
}
步骤6:为MainActivity添加成员
private Button scanButton;
private ListView bleDeviceListView;
private BLEDeviceListAdapter listViewAdapter;
private BluetoothHandler bluetoothHandler;
private boolean isConnected;
步骤7:在MainActivity.onCreate中设置成员值
scanButton = (Button) findViewById(R.id.scanButton);
bleDeviceListView = (ListView)
findViewById(R.id.bleDeviceListView);
listViewAdapter = new BLEDeviceListAdapter(this);
bluetoothHandler = new BluetoothHandler(this);
bluetoothHandler.setOnConnectedListener(new
OnConnectedListener() {
@Override
public void onConnected(boolean isConnected) {
// TODO Auto-generated method stub
setConnectStatus(isConnected);
}
});
bluetoothHandler.setOnRecievedDataListener(new OnRecievedDataListener() {
@Override
public void onRecievedData(byte[] bytes) {
// TODO Auto-generated method stub
System.out.printf(“REC:”);
for(byte b:bytes)
System.out.printf(“%02X ”, b);
System.out.printf(“ ”);
}
});
步骤8:添加SetConnectStatus Mothod
public void setConnectStatus(boolean isConnected){
this.isConnected = isConnected;
if(isConnected){
showMessage(“Connection successful”);
scanButton.setText(“break”);
}else{
bluetoothHandler.onPause();
bluetoothHandler.onDestroy();
scanButton.setText(“scan”);
}
}
private void showMessage(String str){
Toast.makeText(MainActivity.this, str,
Toast.LENGTH_SHORT).show();
}
步骤9:在ScanOnClick中添加内容
if(!isConnected){
bleDeviceListView.setAdapter(bluetoothHandler.getDeviceListAdapter));
bleDeviceListView.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView parent, View view,
int position, long id) {
String buttonText = (String) ((Button)v).getText();
if(buttonText.equals(“scanning”)){
showMessage(“scanning.。.”){
return ;
}
BluetoothDevice device = bluetoothHandler.getDeviceListAdapter().getItem(position).device;
// connect
bluetoothHandler.connect(device.getAddress());
}
});
bluetoothHandler.setOnScanListener(new OnScanListener() {
@Override
public void onScanFinished() {
// TODO Auto-generated method stub
((Button)v).setText(“scan”);
((Button)v).setEnabled(true);
}
@Override
public void onScan(BluetoothDevice device, int rssi, byte[] scanRecord) {}
});
((Button)v).setText(“scanning”);
((Button)v).setEnabled(false);
bluetoothHandler.scanLeDevice(true);
}else{
setConnectStatus(false);
}
步骤10:发送数据
byte[] data = new byte[1];
data[0] = 0x02;
bluetoothHandler.sendData(data);
步骤11:接收数据
在接收到数据之后,
从bluetoothHandler.setOnRecievedDataListener()OnRecievedDataListener.onRecievedData(byte [] bytes)设置的OnRecievedDataListener.onRecievedData(byte [] bytes),字节表示接收到的数据
步骤12 :通过协议将数据发送到MCU。(在ElecFreaks中使用BLUNO)
在src目录中,创建Transmitter.java,ad用以下两个参数确定构造函数:
public Transmitter(Context context,
BluetoothHandler bluetoothHandler){
this.context = context;
this.mBluetoothHandler = bluetoothHandler;
}
如何添加sendData()?
private void sendData(byte[] bytes){
mBluetoothHandler.sendData(bytes);
}
步骤13:接收MCU协议数据
MCU数据接收和发送协议使用JSON数据包,格式为{“ T”:您的值,“ V”:您的值,…}。当然,您可以定义其他值。在src目录中创建MyArray.java,以连接两个阵列。代码如下所示:
public class MyArray {
static public byte[] arrayCat(byte[] buf1,byte[] buf2){
byte[] bufret=null;
int len1 = 0;
int len2 = 0;
if(buf1 != null)
len1 = buf1.length;
if(buf2 != null)
len2 = buf2.length;
if(len1+len2 》 0)
bufret = new byte[len1+len2];
if(len1 》 0)
System.arraycopy(buf1, 0, bufret, 0, len1);
if(len2 》 0)
System.arraycopy(buf2, 0, bufret, len1, len2);
return bufret;
}
}
将示例代码中的protocol.java复制到src目录中,添加成员
private Protocol protocol
从onCreate(),删除:
bluetoothHandler.setOnRecievedDataListener();
添加:
protocol = new Protocol(this, new Transmitter(this, bluetoothHandler));
protocol.setOnReceivedDataListener(recListener);
在MainActivity中添加成员:
private static final boolean INPUT = false;
private static final boolean OUTPUT = true;
private static final boolean LOW = false;
private static final boolean HIGH = true;
private boolean digitalVal[];
private int analogVal[];
在onCreate中初始化:
digitalVal = new boolean[14];
analogVal = new int[14];
private OnReceivedRightDataListener recListener = new
OnReceivedRightDataListener() {
@Override
public int onReceivedData(String str) {
// TODO Auto-generated method stub
try {
JSONObject readJSONObject = new JSONObject(str);
int type = readJSONObject.getInt(“T”);
int value = readJSONObject.getInt(“V”);
switch(type){
case Protocol.ANALOG:{
int pin = readJSONObject.getInt(“P”);
analogVal[pin] = value;
}break;
case Protocol.DIGITAL:{
int pin = readJSONObject.getInt(“P”);
digitalVal[pin] = (value》0)?HIGH:LOW;
}break;
case Protocol.TEMPERATURE:{
float temperature = ((float)value)/100;
}break;
case Protocol.HUMIDITY:{
float humidity = ((float)value)/100;
}break;
default:break;
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
};
步骤14:使用协议发送数据
protocol.writeAnalogData(9, 20);
protocol.writeDigitalData(3, 1);
步骤15:使用协议接收数据
protocol.readAnalogDataCommand(9);
protocol.readDigitalDataCommand(3);
注意:返回的数据由recListener接收
请参阅提供的AndroidIOControl的示例代码。
责任编辑:wv
-
Android
+关注
关注
12文章
3939浏览量
127575
发布评论请先 登录
相关推荐
评论