MinewKeyfinder 开发套件说明
新建工程
Android Studio配置
targetSdkVersion版本选择 21 将minewDevice.jar包放入道libs文件夹下,然后在当前工程下的build.gradle文件配置项中的dependencies新增内容,如下compile files('libs/minewDevice.jar')
Eclipse配置
targetSdkVersion版本选择 21 将minewDevice.jar包放入道libs文件夹下,右击工程propeties,选择Java build Path,在Library选项中添加minewDevice依赖关系
当前SDK需要的权限:
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL"/>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
2
3
4
5
6
7
8
9
10
11
添加service和receiver,如下
<service android:name="com.minew.device.service.ConnectService"/>
<receiver android:name="com.minew.device.BluetoothChangedReceiver">
<intent-filter>
<action android:name="android.bluetooth.adapter.action.STATE_CHANGED"/>
</intent-filter>
</receiver>
2
3
4
5
6
AndroidStudio build.gradle 添加如下依赖: dependencies {
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:recyclerview-v7:23.3.0'
compile 'com.google.code.gson:gson:2.2.4'
compile 'org.greenrobot:eventbus:3.0.0'
compile files('libs/minewDevice.jar')
2
3
4
5
6
} 具体实现细节,请参考demo。
本防丢器SDK提供三个类来获取防丢器信息以及对防丢器进行管理,MinewDeviceManager是所有设备的管理类,MinewDevice是设备实例类,MinewDeviceValue是设备的信息数据类。所有的事件回调采用代理模式。关于各个类的详述请看以下对应部分。注意不要随意的停止扫描,只有当确定所有的设备都已连接上的时候才停止扫描。当有设备断开时要重新开启扫描,否则不能重连。
开始使用
设备管理
MinewDeviceManager(以下简称Manager)类是一个单例管理类,用来管理扫描/连接/绑定设备以及设备的状态更新等,当然,如果设备有诸如连接状态改变之类的事件,也可以从此类获取到回调。
1.初始化Manager,配置代理:
MinewDeviceManager manager = MinewDeviceManager.getInstance(this);
2.发起扫描,检索周围的设备:
manager.startScan();
PS:如果用户手动关闭了蓝牙,可以通过监听MinewDeviceManagerListener的回调方法获取到状态改变,state为蓝牙状态
manager.setMinewDeviceManagerListener(MinewDeviceManagerListener minewDeviceManagerListener);
void onUpdateBluetoothState:(BluetoothState state){}
2
3
4
5
6
发起扫描后,当Manager检测到周围的设备,会为这些设备依次创建一个MinewDevice实例,同时通过以下三个代理方法回调。
// 只要Manager发现周围的设备此方法即进行回调,devices参数包括所有被扫描过的设备
void onScanDevices(List<MinewDevice> devices);
// 此方法仅当设备消失时才进行回调(PS:我们规定,如果被扫描的设备10秒内没有再次被扫描到,那就是消失了。)
void onDisappearDevices(List<MinewDevice> devices);
// 此方法仅当新出现设备时才进行回调(PS:我们规定,如果设备第一次被扫描到或者之前消失过再次被扫描到都将当作新出现设备。)
void onAppearDevices(List<MinewDevice> devices);
2
3
4
5
6
7
8
当然,开发者也可以使用如下方式主动获取当前扫描到的所有设备:
List<MinewDevice> allDevices = MinewDeviceManager.scannedDevices;
3.绑定设备:
只有设备被绑定后,我们才对设备的状态进行持续更新,特别是设备与手机之间的连接状态更新。
manager.bind(minewDevice);
同时,开发者还可以用以下方式获取到之前绑定过的所有设备:
List<MinewDevice> allBindDevices = MinewDeviceManager.bindDevices;
4.连接到设备:
我们提供了主动连接的API,同时通过代理方法监听设备的连接状态改变:
manager.connect(minewDevice);
如果设备发生连接状态的改变,将通过以下方法回调给开发者:
// 当设备的连接状态发生改变,此方法将会回调
- void onDeviceChangeStatus(MinewDevice device,DeviceLinkStatus status);
2
针对已经绑定的设备,我们提供了持续更新设备数据的回调
// 当绑定的设备发生数据改变时,此方法将会回调。
void onUpdateBindDevice:(List<MinewDevice> device);
2
工作开关
2.1.1版本加入的控制开关。
private boolean disableAutoProcessing;
此属性默认为false,如果需要停止自动工作,需要设置为true,只要对此属性进行Set操作,相关工作状态将会立即生效。另外值得注意的是。
警告:如果您不清晰此属性所带来的影响,请不要随意修改。
更多详情请见Demo。
单个设备
对于单个设备,我们为每个设备生成一个MinewDevice(以下简称Device)实例,每个实例包含了设备的信息,以及对设备的操作方法和相关回调。
关于设备信息,目前采用键值对的方式获取。比如获取Device的mac地址:
// 获取MAC地址的value
MinewDeviceValue value = minewDevice.getValue(ValueIndex.ValueIndex_MacAddress);
// 获取MAC地址
String macAddress = value.getStringValue;
2
3
4
5
你可能注意到了,这里的 minewDevice.getValue(ValueIndex.ValueIndex_MacAddress)方法返回的是一个MinewValue(以下简称value)类实例,value实例是对多种数据类型的进一步封装,比如bool/integer/string/float等。关于Device信息的对照,后文有更详细的说明。
对于Device实例某项数据进行修改参见如下代码:
// 生成一个MinewValue实例
MinewDeviceValue value = MinewDeviceValue.index(ValueIndex.ValueIndex_Connected, true);
2
如果要修改的数据是布尔型/整型/浮点型,请按照如下示例进行:
// 配置设备断开报警的延迟时间
minewDevice.setValue(MinewDeviceValue.index(ValueIndex.ValueIndex_AlarmDelay, deleyValue));
// 配置设备断开是否报警
minewDevice.setValue(MinewDeviceValue.index(ValueIndex.ValueIndex_DeviceLoseAlert, loseAlert));
2
3
4
5
我们提供了一些指令,用来控制设备上的一些特性,比如,查找设备,指令发送成功后设备将会响铃
minewDevice.sendInstruction(InstrucIndex_Search);
当指令发送完成后,同样能从回调中获取指令是否成功发送的回调
// index参数是对应的指令枚举,device是某个设备对应的实例,success是布尔值,标注是否成功。
void onSendData(MinewDevice minewDevice, InstrucIndex index, boolean success);
2
当然,还有一些事件不是主动触发的,比如设备向手机发送了一个指令,我们可以使用以下方式监听来自设备端的指令:
// index参数是设备发来的指令枚举
void onReceiveInstructionfromDevice(InstrucIndex index, MinewDevice device);
2
OK,至此你已经可以着手开发了,我们提供了一份Demo代码,你可以对MinewDeviceManager和MinewDevice进行二次封装,用来保证准确接受到每一次的事件回调,当然,这样的示例在Demo里也是有的。
关于MinewDeviceValue的附加说明
通过设备的属性可以获取到对应类型的值,比如
设备名称:String name = aDeviceValue.stringValue
电池电量:int battery = aDeviceValue.intValue;
是否绑定:boolean bind = aDeviceValue.boolValue;
DeviceValue实例共有如下几种属性,可以获取到对应类型的数据,
// 数据对应的枚举
pirvate ValueIndex index;
// 获取整形数据
private int intValue;
// 获取浮点型数据
private float floatValue;
// 获取字符串型数据
private String stringValue;
// 获取16进制data型数据
private byte[] dataValue;
// 获取bool类型数据
private boolean boolValue;
如果需要修改设备的某项信息,可以自行生成一个Value实例
// 生成一个MinewDeviceValue实例
public <T> MinewDeviceValue index(ValueIndex index, T t){}
比如创建名字实例
MinewDeviceValue nameValue = MinewDeviceValue.index(ValueIndex_Name,"手机");
创建设备模式实例
MinewDeviceValue modeValue = MinewDeviceValue.index(ValueIndex_Mode,2);
另外需要注意的是,所有的数据在获取以及生成时,必须严格按照以下对应关系进行。另外并非全部信息都可以被修改,请看下表:
设备属性 | 说明 | 数据类型 | 读写权限 | 备注 |
---|---|---|---|---|
ValueIndex_Name | 自定义的设备名 | stringValue | 可读写 | |
ValueIndex_HeadImage | 设备头像 | dataValue | 可读写 | |
ValueIndex_DeviceId | 从设备读取到的名称 | stringValue | 只读 | 蓝牙返回的设备名 |
ValueIndex_MacAddress | Mac地址 | stringValue | 只读 | |
ValueIndex_Rssi | 信号强度 | intValue | 只读 | |
ValueIndex_Mode | 设备模式 | intValue | 可读写 | |
ValueIndex_Distance | 设备距离 | floatValue | 只读 | |
ValueIndex_Battery | 电池电量 | intValue | 只读 | |
ValueIndex_Bind | 绑定状态 | boolValue | 只读 | |
ValueIndex_DisappearTime | 断开时间 | stringValue | 只读 | 格式:yyyy-MM-dd hh:mm:ss |
ValueIndex_DisappearLong | 断开经度 | floatValue | 只读 | |
ValueIndex_DisappearLati | 断开纬度 | floatValue | 只读 | |
ValueIndex_Connected | 连接状态 | boolValue | 只读 | |
ValueIndex_DeviceLoseAlert | 断开报警 | boolValue | 可读写 | 标记设备断开时是否响铃 |
ValueIndex_Search | 查找状态 | boolValue | 可读写 | 用于UI更新,设备是否被查找 |
ValueIndex_AppLoseAlert | 断开通知 | boolValue | 可读写 | 用于标记设备断开时手机端是否需要反馈 |
ValueIndex_FeatureSupport | 设备是否支持参数调整 | boolValue | 只读 | 如果设备支持距离调节或者延迟报警调节,这里的值为YES |
ValueIndex_AlarmDistance | 调节设备的报警距离 | intValue | 可读写 | 一共有1-8,8个档位,距离由近到远(仅针对支持的设备) |
ValueIndex_AlarmDelay | 调节设备断开时的报警延迟 | intValue | 可读写 | 0-8秒可调范围 |
更新内容
2018.11.20
修复扫描时异常;
2017.04.26
更新部分文本描述;
2017.04.20
添加工作开关。;
2017.01.06
添加权限说明以及新特性说明。
2016.09.12
Ver 1.0