您的位置首页>硬件>

如何更效率的创建Android BLE应用程序

导读 大家好,我是极客范的本期栏目编辑小友,现在为大家讲解如何更效率的创建Android BLE应用程序问题。步骤1:创建新的机器人项目打开日蚀,打

大家好,我是极客范的本期栏目编辑小友,现在为大家讲解如何更效率的创建Android BLE应用程序问题。

步骤1:创建新的机器人项目

打开日蚀,打开文件-《新-》安卓应用项目,然后在"应用程序名称"编辑框中填写应用程序名称,例如流血的例子或其他。最低必需的软件开发工具包(软件开发工具包)选择API18:安卓4.3,并且目标软件开发工具包(软件开发工具包)也选择API18:安卓4.3,因为蓝牙4.0必须具有安卓4.3版本或更高版本。其他默认保持不变,请继续单击"下一步"按钮,直到出现"完成"按钮,然后单击"完成"按钮。

步骤2:添加权限和服务

在清单文件中添加以下代码:

步骤3:创建列表视图项目布局文件

旨在显示列表视图的每个内容,此处我们使用自定义(自己定义),以便每个列表视图可以显示更多内容,item_list.xml如下所示:

将例如。电气故障。brotherhood of locomotive engineers 火车司机兄弟会的源代码复制到您的项目科学研究委员会目录中,然后在出现错误提示的情况下按按住计算机的ctrl按键键不放键打开文件。

步骤4:修改Activity_main.xml,增加扫描按钮和BleDeviceListView

增加的内容如下所示:

android:id="@ id/scanButton"

Android :布局_宽度=“填充_父项”

Android :布局_高度=“wrap _ content”

Android : onclick="scanOnClick"

android:text="scan"/"

Android : id="@ id/bleDeviceListView"

Android :布局_宽度=“填充_父项”

Android :布局_高度=“wrap _ content”

Android : layout _ align left="id @ scan button"

Android : layout _ under=“@ id/scan button”

Android : layout _ over=“@ id/send button”

步骤5:在MainActivity.java中,添加响应事件的扫描按钮方法

(onClick="scanOnClick")

公共空间扫描点击(最终视图v){ 0

}

步骤6:为主要活动添加成员

私人按钮扫描按钮;

private ListView bleDeviceListView;

private bledevicelestadapter listViewAdapter;

私有BluetoothHandler;

nt: 2em;">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接收

步骤16:MCU端口协议(arduino)

请参阅提供的AndroidIOControl的示例代码。

责任编辑:wv

步骤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接收

步骤16:MCU端口协议(arduino)

请参阅提供的AndroidIOControl的示例代码。

责任编辑:wv

.dfma { position: relative; width: 1000px; margin: 0 auto; } .dfma a::after { position: absolute; left: 0; bottom: 0; width: 30px; line-height: 1.4; text-align: center; background-color: rgba(0, 0, 0, .5); color: #fff; font-size: 12px; content:"广告"; } .dfma img { display: block; }
郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。