Skip to content

Commit

Permalink
QVQ
Browse files Browse the repository at this point in the history
  • Loading branch information
pencilso committed Dec 13, 2017
1 parent 2c70077 commit 09e5980
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 4 deletions.
17 changes: 15 additions & 2 deletions ManyBlue/src/main/java/io/javac/ManyBlue/ManyBlue.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.javac.ManyBlue.callback.BlueGattCallBack;
import io.javac.ManyBlue.code.CodeUtils;
import io.javac.ManyBlue.interfaces.BaseNotifyListener;
import io.javac.ManyBlue.interfaces.Instructions;
import io.javac.ManyBlue.manager.BluetoothGattManager;
import io.javac.ManyBlue.manager.EventManager;
import io.javac.ManyBlue.service.BlueLibraryService;
Expand Down Expand Up @@ -149,8 +150,8 @@ public static void blueConnectDevice(String address, Object tag) {
/**
* 注册设备通道
*
* @param uuidMessage
* @param tag
* @param uuidMessage uuid管理类
* @param tag 标记
*/
public static void blueRegisterDevice(UUIDMessage uuidMessage, Object tag) {
if (uuidMessage == null || tag == null)
Expand All @@ -159,6 +160,7 @@ public static void blueRegisterDevice(UUIDMessage uuidMessage, Object tag) {
EventManager.recePost(notifyMessage);
}


/**
* 向蓝牙写出字符数据 不转换
*
Expand Down Expand Up @@ -311,6 +313,17 @@ public static boolean blueSupport(Context context) {
return false;
}

/**
* 关联指令类
*
* @param instructions
* @param tag
*/
public static void blueRelationInstructions(Class<? extends Instructions> instructions, Object tag) {
if (tag == null) throw new NullPointerException("tag can'not null");
NotifyMessage notifyMessage = NotifyMessage.newInstance().setCode(CodeUtils.SERVICE_SET_INSTRUCTIONS_CLASS).setData(instructions).setTag(tag);
EventManager.recePost(notifyMessage);
}

/**
* 处理监听
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
import java.util.List;
import java.util.UUID;

import io.javac.ManyBlue.ManyBlue;
import io.javac.ManyBlue.bean.CharacteristicValues;
import io.javac.ManyBlue.bean.NotifyMessage;
import io.javac.ManyBlue.bean.UUIDMessage;
import io.javac.ManyBlue.code.CodeUtils;
import io.javac.ManyBlue.interfaces.Instructions;
import io.javac.ManyBlue.manager.BluetoothGattManager;
import io.javac.ManyBlue.manager.EventManager;
import io.javac.ManyBlue.utils.HexUtils;
Expand All @@ -30,7 +32,7 @@ public class BlueGattCallBack extends BluetoothGattCallback {
private BluetoothGattService device_service;//服务通道
private BluetoothGattCharacteristic characteristic_write;//写出的通道
private BluetoothGattCharacteristic characteristic_read;//读取通道

private Instructions instructions;//指令对象

public Object getTag() {
return tag;
Expand Down Expand Up @@ -231,4 +233,8 @@ public BluetoothDevice getDevice() {
public BluetoothGatt getGatt() {
return bluetoothGatt;
}

public void setInstructions(Instructions instructions) {
this.instructions = instructions;
}
}
5 changes: 5 additions & 0 deletions ManyBlue/src/main/java/io/javac/ManyBlue/code/CodeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,9 @@ public class CodeUtils {
* 断开所有设备
*/
public static final int SERVICE_DISCONN_DEVICE_ALL = 0xbf;

/**
* 设置指令类 与设备进行关联
*/
public static final int SERVICE_SET_INSTRUCTIONS_CLASS = 0xbf1;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/**
* 指令类
*/
public abstract class Instructions {
public class Instructions {

private final Object tag;

Expand All @@ -29,4 +29,10 @@ protected void blueWriteDataStr2Hex(String data) {
ManyBlue.blueWriteDataStr2Hex(data, tag);
}

@Override
public String toString() {
return "Instructions{" +
"tag=" + tag +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
Expand All @@ -22,9 +24,11 @@
import io.javac.ManyBlue.bean.UUIDMessage;
import io.javac.ManyBlue.callback.BlueGattCallBack;
import io.javac.ManyBlue.code.CodeUtils;
import io.javac.ManyBlue.interfaces.Instructions;
import io.javac.ManyBlue.manager.BluetoothGattManager;
import io.javac.ManyBlue.manager.EventManager;
import io.javac.ManyBlue.utils.HexUtils;
import io.javac.ManyBlue.utils.LogUtils;

/**
* Created by Pencilso on 2017/7/22.
Expand Down Expand Up @@ -216,6 +220,25 @@ public void onMessageEvent(NotifyMessage notifyMessage) {
callBack.disconnect();
}
break;
case CodeUtils.SERVICE_SET_INSTRUCTIONS_CLASS://收到关联指令类的消息
{
try {
Class<? extends Instructions> i = notifyMessage.getData();
Constructor<? extends Instructions> constructor = i.getConstructor(Object.class);
Instructions instructions = constructor.newInstance(notifyMessage.getTag());
BlueGattCallBack gatt = BluetoothGattManager.getGatt(notifyMessage.getTag());
gatt.setInstructions(instructions);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
break;
}
}

Expand Down
6 changes: 6 additions & 0 deletions ManyBlue/src/test/java/io/javac/ManyBlue/ExampleUnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

import org.junit.Test;

import java.lang.reflect.Constructor;

import io.javac.ManyBlue.callback.BlueGattCallBack;
import io.javac.ManyBlue.interfaces.Instructions;
import io.javac.ManyBlue.manager.BluetoothGattManager;

import static org.junit.Assert.*;

/**
Expand Down

0 comments on commit 09e5980

Please sign in to comment.