国产精品久久久aaaa,日日干夜夜操天天插,亚洲乱熟女香蕉一区二区三区少妇,99精品国产高清一区二区三区,国产成人精品一区二区色戒,久久久国产精品成人免费,亚洲精品毛片久久久久,99久久婷婷国产综合精品电影,国产一区二区三区任你鲁

0
  • 聊天消息
  • 系統消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術視頻
  • 寫文章/發帖/加入社區
會員中心
創作中心

完善資料讓更多小伙伴認識你,還能領取20積分哦,立即完善>

3天內不再提示

如何更效率的創建Android BLE應用程序

454398 ? 來源:網絡整理 ? 作者:網絡整理 ? 2019-12-12 10:00 ? 次閱讀
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

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

聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規問題,請聯系本站處理。 舉報投訴
  • Android
    +關注

    關注

    12

    文章

    4024

    瀏覽量

    133977
收藏 人收藏
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

    評論

    相關推薦
    熱點推薦

    學生適合使用的SOLIDWORKS 云應用程序

    隨著科技的不斷發展,計算機輔助設計(CAD)技術已經成為現代工程教育的重要組成部分。SOLIDWORKS作為一款CAD軟件,其教育版云應用程序為學生提供了強大而靈活的設計平臺。本文將探討
    的頭像 發表于 09-15 10:39 ?775次閱讀
    學生適合使用的SOLIDWORKS 云<b class='flag-5'>應用程序</b>

    藍牙打印機電路怎么設計?芯片如何選型?APP和小程序BLE通訊協議如何制定?

    與藍牙芯片通訊的BLE協議怎么制定?藍牙BLE芯片如何選型?一文給你講解清楚
    的頭像 發表于 09-08 10:02 ?1179次閱讀
    藍牙打印機電路怎么設計?芯片如何選型?APP和小<b class='flag-5'>程序</b>的<b class='flag-5'>BLE</b>通訊協議如何制定?

    谷歌全新Android助力提高開發效率

    隨著 Android 的更新,加上 AI 的接入與賦能,讓 Android 開發新范式正在加速成型。
    的頭像 發表于 08-29 09:22 ?854次閱讀

    CY8C4128LQI-BL543無法掃描PC和Android手機,為什么?

    。 為什么?(PC和Android手機 CAN 廣告和s同時CAN )。 我使用的是 Psoc 4 ble 4.2 版本的設備和 Psoc creator 4.4 版本。 掃描 PC 和 Android 手機需要配置或
    發表于 07-07 08:09

    外圍設備通過手機連接到BLE應用程序,為什么不能連接到Infineaon BLE

    ; key_ch06_ex01_ \" observer 項目創建BLE 中央應用程序。 但是它無法掃描附近藍牙中的任何設備。 它進入高掃描模式 5 秒鐘,進入低掃描模式 5 秒鐘然后停止。 不顯示任何主機 ID,包括我的外
    發表于 07-07 08:06

    如何查找 CYBT-213043-MESH 套件的 BLE 網格參考應用?

    CYBT-213043-EVAL 的 BSP(而不是 CYBT-213043-MESH),但沒有找到參考的 BLE 網狀應用程序)。 如何查找 CYBT-213043-MESH 套件的 BLE 網格參考應用?
    發表于 07-02 07:44

    Android14在BLE中,當MTU超過 517時,如何處理數據傳輸?

    Android 14 更新了BLE 通信的固定MTU(517)。 https://developer.android.com/about/versions/14
    發表于 07-01 06:56

    ArkUI-X與Android消息通信

    平臺與ArkUI交互,ArkUI側具體用法請參考Bridge API,Android側參考BridgePlugin。 創建平臺橋接 1.在ArkUI側創建平臺橋接。指定名稱,該名稱應與Andr
    發表于 06-08 22:15

    BLE連接未啟動的原因?

    我們遇到了與 BLE 連接相關的問題,詳情如下 添加了新的詳細日志(Detailed-logs.zip)。 移動設備:Android Samsung z flip 設備:M63 KA 設備應用程序
    發表于 06-05 07:12

    如何使用CYUSB3KIT-003使用GPIO訪問SRAM的應用程序

    你好。我是CYUSB3的初學者。 我想創建一個使用 CYUSB3KIT-003 使用 GPIO 訪問 SRAM 的應用程序。 目前我已經在我的電腦上安裝了SDK,但是有什么參考資料嗎?
    發表于 05-14 06:51

    如何在i.mx8mq的android13上啟用Widevine DRM?

    Widevine DRM 13 級呢? 附加圖片由“DRM 信息”應用程序檢查。 Android 11 中支持的 Widevine Android 11 中支持的 Widevine
    發表于 04-11 06:44

    【技術案例】Android in Docker

    Docker介紹Docker是一個開源的容器化平臺,用于打包、分發和運行應用程序。它通過將應用及其所有依賴打包到獨立的容器中,確保應用在不同環境中一致運行。Docker提供快速部署、隔離性強和高效
    的頭像 發表于 04-02 16:33 ?1755次閱讀
    【技術案例】<b class='flag-5'>Android</b> in Docker

    物聯網BLE技術助力防丟器方案PTR5618

    隨著物聯網和低功耗藍牙(BLE)技術的快速發展,防丟器方案成為了解決日常生活中物品丟失問題的有效工具。 方案說明: 該防丟器方案基于BLE和物聯網技術,由一個小巧的設備和手機APP應用程序組成。用戶
    發表于 03-18 09:50

    基于BLE技術 智能手寫筆解決方案:改變你的書寫體驗PTR5415

    支持多種移動設備平臺,包括iOS和Android系統,使用戶能夠在不同的設備上自由使用智能手寫筆。 4、應用擴展:通過軟件開發接口(API),我們的方案允許開發者為智能手寫筆設計各種應用程序,如手寫
    發表于 03-11 17:50

    如何部署OpenVINO?工具套件應用程序

    編寫代碼并測試 OpenVINO? 工具套件應用程序后,必須將應用程序安裝或部署到生產環境中的目標設備。 OpenVINO?部署管理器指南包含有關如何輕松使用部署管理器將應用程序打包并部署到目標主機的詳細信息。 注意:Ope
    發表于 03-06 08:23