步驟1:創(chuàng)建新的Android項(xiàng)目
打開(kāi)Eclipse,打開(kāi)File-》 New-》 Android Application Project ,然后在“應(yīng)用程序名稱”編輯框中填寫(xiě)應(yīng)用程序名稱,例如BleExample或其他。最低必需的SDK選擇API18:Android 4.3,并且目標(biāo)SDK也選擇API18:Android 4.3,因?yàn)閎uletooth 4.0必須具有Android 4.3版本或更高版本。其他默認(rèn)保持不變,請(qǐng)繼續(xù)單擊“下一步”按鈕,直到出現(xiàn)“完成”按鈕,然后單擊“完成”按鈕。
步驟2:添加權(quán)限和服務(wù)
在清單文件中添加以下代碼:
步驟3:創(chuàng)建ListView項(xiàng)目布局文件
旨在顯示ListView的每個(gè)內(nèi)容,此處我們使用自定義(自己定義),以便每個(gè)ListView可以顯示更多內(nèi)容,item_list.xml如下所示:
將BleExample/com.elecfreaks.ble的源代碼復(fù)制到您的項(xiàng)目src目錄中,然后在出現(xiàn)錯(cuò)誤提示的情況下按Shift + Ctrl + O鍵打開(kāi)文件。
步驟4:修改Activity_main.xml,增加ScanButton和BleDeviceListView
增加的內(nèi)容如下所示:
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中,添加響應(yīng)事件的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中設(shè)置成員值
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中添加內(nèi)容
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:發(fā)送數(shù)據(jù)
byte[] data = new byte[1];
data[0] = 0x02;
bluetoothHandler.sendData(data);
步驟11:接收數(shù)據(jù)
在接收到數(shù)據(jù)之后,
從bluetoothHandler.setOnRecievedDataListener()OnRecievedDataListener.onRecievedData(byte [] bytes)設(shè)置的OnRecievedDataListener.onRecievedData(byte [] bytes),字節(jié)表示接收到的數(shù)據(jù)
步驟12 :通過(guò)協(xié)議將數(shù)據(jù)發(fā)送到MCU。(在ElecFreaks中使用BLUNO)
在src目錄中,創(chuàng)建Transmitter.java,ad用以下兩個(gè)參數(shù)確定構(gòu)造函數(shù):
public Transmitter(Context context,
BluetoothHandler bluetoothHandler){
this.context = context;
this.mBluetoothHandler = bluetoothHandler;
}
如何添加sendData()?
private void sendData(byte[] bytes){
mBluetoothHandler.sendData(bytes);
}
步驟13:接收MCU協(xié)議數(shù)據(jù)
MCU數(shù)據(jù)接收和發(fā)送協(xié)議使用JSON數(shù)據(jù)包,格式為{“ T”:您的值,“ V”:您的值,…}。當(dāng)然,您可以定義其他值。在src目錄中創(chuàng)建MyArray.java,以連接兩個(gè)陣列。代碼如下所示:
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復(fù)制到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:使用協(xié)議發(fā)送數(shù)據(jù)
protocol.writeAnalogData(9, 20);
protocol.writeDigitalData(3, 1);
步驟15:使用協(xié)議接收數(shù)據(jù)
protocol.readAnalogDataCommand(9);
protocol.readDigitalDataCommand(3);
注意:返回的數(shù)據(jù)由recListener接收
請(qǐng)參閱提供的AndroidIOControl的示例代碼。
責(zé)任編輯:wv
-
Android
+關(guān)注
關(guān)注
12文章
3946瀏覽量
128207
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
基于HPM_SDK_ENV開(kāi)發(fā)應(yīng)用程序的升級(jí)處理

Spire.XLS for Android via Java組件說(shuō)明

android手機(jī)上emulate應(yīng)用程序的方法
使用OpenVINO? ElectronJS中創(chuàng)建桌面應(yīng)用程序

CYC8PROTO-063-BLE是否可以通過(guò)智能手機(jī)上的標(biāo)準(zhǔn)設(shè)置應(yīng)用程序建立連接?
如何在Android 10設(shè)備上通過(guò)App控制GPIO
Anthropic推出Claude Android應(yīng)用程序,挑戰(zhàn)ChatGPT市場(chǎng)地位
CYC8PROTO-063-BLE嘗試連接AIROC移動(dòng)應(yīng)用程序時(shí),TeraTerm中就會(huì)出現(xiàn)大量錯(cuò)誤信息,為什么?
安裝在Android 14上的LE OTA應(yīng)用程序無(wú)法使用是怎么回事?
使用Android9操作系統(tǒng)測(cè)試BLE功能,BLE連接失敗的原因?
CYW20829從Eclipse為Modustoolbox創(chuàng)建\"新應(yīng)用程序時(shí)出現(xiàn)錯(cuò)誤\"的原因?
【從0開(kāi)始創(chuàng)建AWTK應(yīng)用程序】編譯應(yīng)用到RTOS平臺(tái)

評(píng)論