在线观看www成人影院-在线观看www日本免费网站-在线观看www视频-在线观看操-欧美18在线-欧美1级

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評(píng)論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫(xiě)文章/發(fā)帖/加入社區(qū)
會(huì)員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識(shí)你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

如何更效率的創(chuàng)建Android BLE應(yīng)用程序

454398 ? 來(lái)源:網(wǎng)絡(luò)整理 ? 作者:網(wǎng)絡(luò)整理 ? 2019-12-12 10:00 ? 次閱讀

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

步驟16:MCU端口協(xié)議(arduino

請(qǐng)參閱提供的AndroidIOControl的示例代碼。

責(zé)任編輯:wv

聲明:本文內(nèi)容及配圖由入駐作者撰寫(xiě)或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場(chǎng)。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問(wèn)題,請(qǐng)聯(lián)系本站處理。 舉報(bào)投訴
  • Android
    +關(guān)注

    關(guān)注

    12

    文章

    3946

    瀏覽量

    128207
收藏 人收藏

    評(píng)論

    相關(guān)推薦

    基于HPM_SDK_ENV開(kāi)發(fā)應(yīng)用程序的升級(jí)處理

    )以及工程創(chuàng)建工具等文件。用戶基于HPM_SDK_ENV開(kāi)發(fā)自己的應(yīng)用程序時(shí)需要考慮如何維護(hù)板級(jí)配置文件和應(yīng)用程序文件的問(wèn)題。以下3種維護(hù)方式:用戶將自己的板級(jí)配置文
    的頭像 發(fā)表于 02-08 13:38 ?629次閱讀
    基于HPM_SDK_ENV開(kāi)發(fā)<b class='flag-5'>應(yīng)用程序</b>的升級(jí)處理

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

    Spire.XLS for Android via Java 是一款專業(yè)的 Android Excel 組件,用于在 Android 手機(jī)應(yīng)用程序
    的頭像 發(fā)表于 01-24 12:16 ?139次閱讀
    Spire.XLS for <b class='flag-5'>Android</b> via Java組件說(shuō)明

    android手機(jī)上emulate應(yīng)用程序的方法

    Android手機(jī)上模擬(emulate)應(yīng)用程序的方法通常涉及到使用Android模擬器(Emulator)或類似的工具來(lái)模擬Android環(huán)境,以便在沒(méi)有實(shí)際物理設(shè)備的情況下運(yùn)行
    的頭像 發(fā)表于 12-05 15:33 ?574次閱讀

    使用OpenVINO? ElectronJS中創(chuàng)建桌面應(yīng)用程序

    的用戶體驗(yàn)。 1 應(yīng)用程序概覽:一種簡(jiǎn)單的背景虛化方法 這個(gè)演示展示了如何在 Node.js 環(huán)境中使用 OpenVINO 工具包實(shí)現(xiàn)背景虛化,并通過(guò) Electron.js 創(chuàng)建的直觀桌面界面進(jìn)行
    的頭像 發(fā)表于 11-25 11:35 ?284次閱讀
    使用OpenVINO? ElectronJS中<b class='flag-5'>創(chuàng)建</b>桌面<b class='flag-5'>應(yīng)用程序</b>

    CYC8PROTO-063-BLE是否可以通過(guò)智能手機(jī)上的標(biāo)準(zhǔn)設(shè)置應(yīng)用程序建立連接?

    你好,我是使用 BLE 的新手,正在使用 CYC8PROTO-063-BLE 板,我想知道是否可以通過(guò)智能手機(jī)(iOS 或 Android)上的標(biāo)準(zhǔn)設(shè)置應(yīng)用程序建立連接。 到目前為止,
    發(fā)表于 07-23 08:20

    如何在Android 10設(shè)備上通過(guò)App控制GPIO

    本文檔提供了在 Android 10 設(shè)備上通過(guò)應(yīng)用程序(App)控制通用輸入輸出(GPIO)的詳細(xì)指南。這涵蓋了從創(chuàng)建 gpio驅(qū)動(dòng)到App 配置 以及 SELinux 策略以允許特定訪問(wèn)的所有必要步驟。
    的頭像 發(fā)表于 07-22 17:59 ?1417次閱讀

    Anthropic推出Claude Android應(yīng)用程序,挑戰(zhàn)ChatGPT市場(chǎng)地位

    在人工智能領(lǐng)域的激烈競(jìng)爭(zhēng)中,新興力量Anthropic于本周二正式推出了其備受矚目的Claude Android應(yīng)用程序,標(biāo)志著這家公司在多平臺(tái)布局上邁出了重要一步,旨在通過(guò)提供更廣泛、更便捷的訪問(wèn)方式,吸引并留住用戶,從而在與ChatGPT等巨頭的競(jìng)爭(zhēng)中脫穎而出。
    的頭像 發(fā)表于 07-17 17:24 ?1146次閱讀

    CYC8PROTO-063-BLE嘗試連接AIROC移動(dòng)應(yīng)用程序時(shí),TeraTerm中就會(huì)出現(xiàn)大量錯(cuò)誤信息,為什么?

    我目前正在使用 CYC8PROTO-063-BLE 套件,并試圖在 ModusToolBox 3.2 中運(yùn)行 Bluetoothe_LE_Findme 示例代碼。 程序在電路板上運(yùn)行正常,但每當(dāng)
    發(fā)表于 07-05 06:03

    安裝在Android 14上的LE OTA應(yīng)用程序無(wú)法使用是怎么回事?

    你好。 LE OTA 應(yīng)用程序示例代碼無(wú)法在 Android 14 智能手機(jī)上運(yùn)行。 點(diǎn)擊更新按鈕時(shí),顯示"abort" ,OTA 進(jìn)程無(wú)法繼續(xù)。 13 及以下版本沒(méi)有問(wèn)題。 為什么會(huì)這樣?
    發(fā)表于 07-05 06:01

    使用Android9操作系統(tǒng)測(cè)試BLE功能,BLE連接失敗的原因?

    我們正在使用 Android9 操作系統(tǒng)測(cè)試 BLE 功能。 在測(cè)試過(guò)程中,我們發(fā)現(xiàn) BLE 連接失敗,錯(cuò)誤顯示\" bt_smp[i]: dhkey chcks do nomatch
    發(fā)表于 05-27 06:10

    CYW20829從Eclipse為Modustoolbox創(chuàng)建\"新應(yīng)用程序時(shí)出現(xiàn)錯(cuò)誤\"的原因?

    CYW20829]從 Eclipse 為 Modustoolbox 創(chuàng)建\"新應(yīng)用程序時(shí)出現(xiàn)錯(cuò)誤\"
    發(fā)表于 05-22 06:12

    【從0開(kāi)始創(chuàng)建AWTK應(yīng)用程序】編譯應(yīng)用到RTOS平臺(tái)

    AWTK是基于C語(yǔ)言開(kāi)發(fā)的跨平臺(tái)GUI框架。本系列文章介紹如何從0開(kāi)始創(chuàng)建AWTK應(yīng)用程序,包括搭建開(kāi)發(fā)調(diào)試環(huán)境、使用AWTK創(chuàng)建Hello工程并在模擬器上運(yùn)行、將AWTK應(yīng)用程序移植
    的頭像 發(fā)表于 03-21 08:23 ?717次閱讀
    【從0開(kāi)始<b class='flag-5'>創(chuàng)建</b>AWTK<b class='flag-5'>應(yīng)用程序</b>】編譯應(yīng)用到RTOS平臺(tái)

    ST BLE Toolbox Android下載打不開(kāi)怎么解決?

    在官網(wǎng)上下載ST BLE Toolbox-Android,但是發(fā)現(xiàn)半天都打不開(kāi)網(wǎng)站,嘗試了點(diǎn)開(kāi)IOS的卻一下子就進(jìn)去了,這是什么問(wèn)題哦
    發(fā)表于 03-18 06:45

    Psoc4 ble設(shè)備無(wú)法掃描PC和安卓手機(jī)是為什么?

    。 為什么?(PC和Android手機(jī) CAN 廣告和s同時(shí)CAN )。 我使用的是 Psoc 4 ble 4.2 版本的設(shè)備和 Psoc creator 4.4 版本。 掃描 PC 和 Android 手機(jī)需要配置或
    發(fā)表于 03-01 11:30

    將Wiced 6.6用于BLE應(yīng)用程序,執(zhí)行10字節(jié)的簡(jiǎn)單讀取操作時(shí)需要超過(guò)100毫秒的原因?

    我正在將 Wiced 6.6 用于我們現(xiàn)有的基于 BLE應(yīng)用程序之一。 本產(chǎn)品使用了 CYW4343 無(wú)線藍(lán)牙組合模塊。 我已將設(shè)備配置為服務(wù)器。 在客戶端執(zhí)行 10 字節(jié)的簡(jiǎn)單讀取操作時(shí),即使
    發(fā)表于 03-01 08:10
    主站蜘蛛池模板: 天天更新影院 | 成人国产三级在线播放 | 天堂社区在线观看 | 污污的网站免费阅读 | 黄色午夜影院 | 国产区亚洲区 | 午夜免费片 | 国产福利资源 | aaa在线 | 四虎永久免费在线观看 | 好爽好紧好大的免费视频国产 | 亚洲一区欧美日韩 | a级毛片毛片免费很很综合 a级男女性高爱潮高清试 | china国语对白刺激videos chinese国产videoxx实拍 | 丁香花五月婷婷 | 一级一级毛片免费播放 | 成人午夜大片免费看爽爽爽 | 最新欧美伦理网 | 国产精品福利一区二区亚瑟 | 亚洲成a人一区二区三区 | 东京毛片 | 午夜在线视频观看 | 亚洲五月六月丁香激情 | 中文字幕一区二区三区在线观看 | 国产亚洲精品在天天在线麻豆 | 草草影院www色极品欧美 | 四虎永久精品免费观看 | 色www 永久免费网站 | 中文在线最新版天堂bt | 在线播放真实国产乱子伦 | 在线视频 一区二区 | 亚洲一卡二卡三卡 | 免费看欧美理论片在线 | 四虎影院永久免费观看 | 99精品久久99久久久久久 | 日韩欧美一级 | 深爱五月网| 天天舔天天色 | 日本特级黄色大片 | 天堂在线中文字幕 | 亚洲xx网站 |