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

    文章

    3849

    浏览量

    125617
收藏 人收藏

    评论

    相关推荐

    谷歌发布新的AI SDK,简化Gemini模型与Android应用程序的集成

    对于 Android 应用程序,Google 提供了 Google AI Client SDK for Android,它将 Gemini REST API 封装为惯用的 Kotlin API
    的头像 发表于 01-03 16:29 517次阅读

    【从0开始创建AWTK应用程序创建应用程序并在模拟器运行

    AWTK是基于C语言开发的跨平台GUI框架。本系列文章介绍如何从0开始创建AWTK应用程序,包括搭建开发调试环境、使用AWTK创建Hello工程并在模拟器上运行、将AWTK应用程序移植
    的头像 发表于 12-01 08:24 216次阅读
    【从0开始<b class='flag-5'>创建</b>AWTK<b class='flag-5'>应用程序</b>】<b class='flag-5'>创建</b><b class='flag-5'>应用程序</b>并在模拟器运行

    【从0开始创建AWTK应用程序】开发及调试环境搭建

    AWTK是基于C语言开发的跨平台GUI框架。本系列文章介绍如何从0开始创建AWTK应用程序,包括搭建开发调试环境、使用AWTK创建Hello工程并在模拟器上运行、将AWTK应用程序移植
    的头像 发表于 11-11 10:00 285次阅读
    【从0开始<b class='flag-5'>创建</b>AWTK<b class='flag-5'>应用程序</b>】开发及调试环境搭建

    如何让STM32WB在没有LSE时运行BLE应用程序

    如何让STM32WB在没有LSE时运行BLE应用程序
    的头像 发表于 10-24 16:41 224次阅读
    如何让STM32WB在没有LSE时运行<b class='flag-5'>BLE</b><b class='flag-5'>应用程序</b>

    用于创建闪存LED任务的FreeRTOS应用程序

    应用程序 : 本代码是 FreeRTOS 任务的基本应用程序, 用于创建闪存 LED 任务 。 BSP 版本: M480系列 BSP CMSIS V3.04.000 硬件
    发表于 08-30 07:55

    FreeRTOS任务创建到闪存LED的基本应用程序

    应用程序 : 本代码是 FreeRTOS 任务的基本应用程序, 用于创建闪存 LED 任务 。 BSP 版本: M480系列 BSP CMSIS V3.04.000 硬件
    发表于 08-22 06:28

    带ESP32和TM1638的记分牌,带BLE Android应用程序

    电子发烧友网站提供《带ESP32和TM1638的记分牌,带BLE Android应用程序.zip》资料免费下载
    发表于 07-12 10:57 0次下载
    带ESP32和TM1638的记分牌,带<b class='flag-5'>BLE</b> <b class='flag-5'>Android</b><b class='flag-5'>应用程序</b>

    使用Arduino和处理Android应用程序DIY车速表

    电子发烧友网站提供《使用Arduino和处理Android应用程序DIY车速表.zip》资料免费下载
    发表于 07-06 11:23 0次下载
    使用Arduino和处理<b class='flag-5'>Android</b><b class='flag-5'>应用程序</b>DIY车速表

    创建端到端零售愿景AI应用程序

    使用 NVIDIA DeepStream 和 NVIDIA TAO 工具包构建端到端零售分析应用程序
    的头像 发表于 07-05 16:30 253次阅读
    <b class='flag-5'>创建</b>端到端零售愿景AI<b class='flag-5'>应用程序</b>

    Android手机应用程序来控制调频收音机的创建

    电子发烧友网站提供《用Android手机应用程序来控制调频收音机的创建.zip》资料免费下载
    发表于 06-12 09:36 4次下载
    用<b class='flag-5'>Android</b>手机<b class='flag-5'>应用程序</b>来控制调频收音机的<b class='flag-5'>创建</b>

    无法在Nanoleaf Android应用程序上找到OTBR怎么解决?

    我正在使用 Nanoleaf Essential Matter A19 | 通过 Nanoleaf Android 应用程序在 NXP 的 iMX93 和 iMX8 参考板上使用我的自定义 OTBR
    发表于 06-08 08:23

    如何通过Android应用程序在NodeMCU V2上点亮LED ?

    正如标题所说,我正在努力解决这个问题,我知道网上可能有关于我如何做到这一点的教程,但对于我来说,我无法找到我正在寻找的东西。我想知道如何通过 Android 应用程序(使用 Android
    发表于 06-06 12:23

    使用esp在android上运行应用程序或文件?

    我是初学者,我使用 Arduino 和一些我知道的传感器 但是,我想知道我是否可以使用我的 android 手机作为输出?例如,我想在特定传感器检测到某些东西时播放视频。 即使不运行视频,我也可以制作一个自动播放视频的应用程序,但我可以使用 esp 运行该
    发表于 06-05 06:49

    NanoBeacon™ BLE扫描器移动应用程序上架应用宝

    NanoBeacon BLE扫描器移动应用程序是一个先进的BLE扫描器,允许你发现广告BLE设备并观察其广告数据。它是我们的NanoBeacon IN100设备的完美伴侣
    的头像 发表于 05-22 11:12 495次阅读
    NanoBeacon™ <b class='flag-5'>BLE</b>扫描器移动<b class='flag-5'>应用程序</b>上架应用宝

    NanoBeacon™ BLE扫描器移动应用程序上架应用宝

    2023年5月,NanoBeacon™BLE扫描器移动应用程序正式上架应用宝,欢迎大家关注并下载。简介:NanoBeacon™BLEAPP由上海橙群微电子有限公司独立开发,此APP允许你扫描和发现
    的头像 发表于 05-22 10:39 280次阅读
    NanoBeacon™ <b class='flag-5'>BLE</b>扫描器移动<b class='flag-5'>应用程序</b>上架应用宝