chinese直男口爆体育生外卖, 99久久er热在这里只有精品99, 又色又爽又黄18禁美女裸身无遮挡, gogogo高清免费观看日本电视,私密按摩师高清版在线,人妻视频毛茸茸,91论坛 兴趣闲谈,欧美 亚洲 精品 8区,国产精品久久久久精品免费

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

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

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

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

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

掃碼添加小助手

加入工程師交流群

步驟1:創(chuàng)建新的Android項目

打開Eclipse,打開File-》 New-》 Android Application Project ,然后在“應(yīng)用程序名稱”編輯框中填寫應(yīng)用程序名稱,例如BleExample或其他。最低必需的SDK選擇API18:Android 4.3,并且目標(biāo)SDK也選擇API18:Android 4.3,因為buletooth 4.0必須具有Android 4.3版本或更高版本。其他默認保持不變,請繼續(xù)單擊“下一步”按鈕,直到出現(xiàn)“完成”按鈕,然后單擊“完成”按鈕。

步驟2:添加權(quán)限和服務(wù)

在清單文件中添加以下代碼:

步驟3:創(chuàng)建ListView項目布局文件

旨在顯示ListView的每個內(nèi)容,此處我們使用自定義(自己定義),以便每個ListView可以顯示更多內(nèi)容,item_list.xml如下所示:

將BleExample/com.elecfreaks.ble的源代碼復(fù)制到您的項目src目錄中,然后在出現(xiàn)錯誤提示的情況下按Shift + Ctrl + O鍵打開文件。

步驟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 :通過協(xié)議將數(shù)據(jù)發(fā)送到MCU。(在ElecFreaks中使用BLUNO)

在src目錄中,創(chuàng)建Transmitter.java,ad用以下兩個參數(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,以連接兩個陣列。代碼如下所示:

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

請參閱提供的AndroidIOControl的示例代碼。

責(zé)任編輯:wv

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

    關(guān)注

    12

    文章

    3980

    瀏覽量

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

掃碼添加小助手

加入工程師交流群

    評論

    相關(guān)推薦
    熱點推薦

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

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

    外圍設(shè)備通過手機連接到BLE應(yīng)用程序,為什么不能連接到Infineaon BLE

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

    如何查找 CYBT-213043-MESH 套件的 BLE 網(wǎng)格參考應(yīng)用?

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

    BLE連接未啟動的原因?

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

    如何使用CYUSB3KIT-003使用GPIO訪問SRAM的應(yīng)用程序?

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

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

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

    Spire.XLS for Android via Java組件說明

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

    BQ78412應(yīng)用程序編程接口

    電子發(fā)燒友網(wǎng)站提供《BQ78412應(yīng)用程序編程接口.pdf》資料免費下載
    發(fā)表于 12-18 14:46 ?0次下載
    BQ78412<b class='flag-5'>應(yīng)用程序</b>編程接口

    STM32WB55RG開發(fā)(3)----生成 BLE 程序連接手機APP

    本項目旨在利用 STM32WB55 微控制器的藍牙低功耗(BLE)功能,實現(xiàn)與手機 APP 的無線連接。通過配置時鐘源、啟動關(guān)鍵模塊(如 RCC、RTC、RF、IPCC 和 HSEM),以及啟用藍牙功能,用戶可以創(chuàng)建一個穩(wěn)定的 BLE
    的頭像 發(fā)表于 12-16 16:06 ?2358次閱讀
    STM32WB55RG開發(fā)(3)----生成 <b class='flag-5'>BLE</b> <b class='flag-5'>程序</b>連接手機APP

    TAS2521應(yīng)用程序參考指南

    電子發(fā)燒友網(wǎng)站提供《TAS2521應(yīng)用程序參考指南.pdf》資料免費下載
    發(fā)表于 12-10 13:49 ?0次下載
    TAS2521<b class='flag-5'>應(yīng)用程序</b>參考指南

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

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

    AWTK-WEB 快速入門(2) - JS 應(yīng)用程序

    導(dǎo)讀AWTK可以使用相同的技術(shù)棧開發(fā)各種平臺的應(yīng)用程序。有時我們需要使用Web界面與設(shè)備進行交互,本文介紹一下如何使用JS語言開發(fā)AWTK-WEB應(yīng)用程序。用AWTKDesigner新建一個應(yīng)用程序先安裝AWTKDesigner
    的頭像 發(fā)表于 12-05 01:04 ?685次閱讀
    AWTK-WEB 快速入門(2) - JS <b class='flag-5'>應(yīng)用程序</b>

    AWTK-WEB 快速入門(1) - C 語言應(yīng)用程序

    導(dǎo)讀AWTK可以使用相同的技術(shù)棧開發(fā)各種平臺的應(yīng)用程序。有時我們需要使用Web界面與設(shè)備進行交互,本文介紹一下如何使用C語言開發(fā)AWTK-WEB應(yīng)用程序。用AWTKDesigner新建一個應(yīng)用程序
    的頭像 發(fā)表于 11-27 11:46 ?1015次閱讀
    AWTK-WEB 快速入門(1) - C 語言<b class='flag-5'>應(yīng)用程序</b>

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

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

    PCM2912應(yīng)用程序的操作環(huán)境

    電子發(fā)燒友網(wǎng)站提供《PCM2912應(yīng)用程序的操作環(huán)境.pdf》資料免費下載
    發(fā)表于 10-21 09:33 ?0次下載
    PCM2912<b class='flag-5'>應(yīng)用程序</b>的操作環(huán)境