1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > android上位机编程 BLE的Android上位机开发(上)

android上位机编程 BLE的Android上位机开发(上)

时间:2019-01-15 20:28:54

相关推荐

android上位机编程 BLE的Android上位机开发(上)

原标题:BLE的Android上位机开发(上)

各位坛友大家好啊!

上篇BLE的Android开发小技巧(见帖:BLE4.0安卓上位机开发小技巧),但因为当时Android上位机还没有完全开发完毕,BleLib开源包也用的不太熟练,因此没有详细的代码说明。随后就是漫长的出差,天天忙忙碌碌的没时间弄。然而,就在昨天,就在昨天~我的Android上位机全部开发完毕啦~~(真是个举国欢庆的日子啊。。)别的不多扯了,进入主题聊聊Blelib这个包怎么用吧。

1、声明对象

这个太简单,直接码。这个mBleService是核心。

[Java] 纯文本查看复制代码

?

1

BleService mBleService= null ;

2、绑定BLELIB服务

这里涉及到两个函数。其中doBindService()是用来绑定BLE服务的,这个在初始化建立蓝牙连接的时候用到;doUnBindService()是用来解绑服务的,在关闭的时候调用。

[Java] 纯文本查看复制代码

?

01

02

03

04

05

06

07

08

09

10

11

12

13

private void doBindService() {

Intent serviceIntent = new Intent( this , BleService. class );

bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE);

mIsBind= true ;

}

private void doUnBindService() {

if (mIsBind) {

unbindService(serviceConnection);

mBleService = null ;

mIsBind = false ;

}

}

3、打开蓝牙扫描设备

这个要介绍一位新人了~~~serviceConnection:

[Java] 纯文本查看复制代码

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

private ServiceConnection serviceConnection = new ServiceConnection() {

@Override

public void onServiceConnected(ComponentName name, IBinder service) {

mBleService = ((BleService.LocalBinder) service).getService();

if (mBleService != null ) setBleServiceListener();

if (mBleService.initialize()) {

if (mBleService.enableBluetooth( true )) {

mBleService.scanLeDevice( true );

Toast.makeText(MainActivity. this , "Bluetooth was opened" , Toast.LENGTH_SHORT).show();

}

} else {

Toast.makeText(MainActivity. this , "not support Bluetooth" , Toast.LENGTH_SHORT).show();

}

}

};

细心的网友肯定发现了,serviceConnection就是doBindService()里面调用的东西,通过重写ServiceConnection()实例化mBleService,并且判断设备是否支持蓝牙,然后开始扫描BLE设备。

观测Android是否还在扫描蓝牙设备,可以用mBleService.isScanning()函数,返回true是仍在扫描,false是扫描完毕。

4、设置各种回调函数

在serviceConnection里面有个调用:setBleServiceListener(); 这个是核心的核心,各种Scan、Connection、Services、Characteristic什么的,重要的信息都在这里。

那么~~抖个包袱,这一部分下次再开帖分享给大家!

感谢大家百忙之中看帖~谢谢大家啦!

helloeeworld为好友,发送职业或EEWORLD论坛账号+感兴趣的方向,经审核通过即会被拉入我们在不用领域建立的微信交流群。返回搜狐,查看更多

责任编辑:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。