MainActivity.java
5.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package com.metroapp;
import android.annotation.TargetApi;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;
import com.facebook.react.ReactActivity;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.WritableMap;
import com.metroapp.bean.ScanResultBean;
import com.metroapp.nativemodules.BluetoothModule;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends ReactActivity {
/**
* Returns the name of the main component registered from JavaScript.
* This is used to schedule rendering of the component.
*/
@Override
protected String getMainComponentName() {
return "metroApp";
}
// 本地蓝牙适配器
private BluetoothAdapter mBluetoothAdapter;
private static ScanResultBean scanResultBean = null;
private static MainActivity _this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
_this = this;
initBluetooth();
}
@TargetApi(22)
public void initBluetooth(){
BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);//这里与标准蓝牙略有不同
mBluetoothAdapter = bluetoothManager.getAdapter();
if (mBluetoothAdapter == null){
Toast.makeText(MainActivity.this,"该手机暂无蓝牙设备",Toast.LENGTH_SHORT).show();
return;
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {//小于18
Toast.makeText(MainActivity.this,"该手机暂不支持蓝牙4.0",Toast.LENGTH_SHORT).show();
return;
}
if (!mBluetoothAdapter.isEnabled()) {
mBluetoothAdapter.enable();
}
mBluetoothAdapter.startLeScan(mLeScanCallback);
}
private static int lastRSS =-9999;//上一个rss值
private static String UUID ="E2C56DB5-DFFB-48D2-B060-D0F5A71096E0";
private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, final int rssi,
final byte[] scanRecord) {
int startByte = 4;
String scanRecordhex=bytesToHex(scanRecord);
String hexString = scanRecordhex.substring(18,50);
// ibeacon的UUID值
String uuid = hexString.substring(0, 8) + "-"
+ hexString.substring(8, 12) + "-"
+ hexString.substring(12, 16) + "-"
+ hexString.substring(16, 20) + "-"
+ hexString.substring(20, 32);
int major = Integer.valueOf(scanRecordhex.substring(50,54),16);
int minor = Integer.valueOf(scanRecordhex.substring(54,58),16);
String ibeaconName = device.getName()==null?"null":device.getName();
String mac = device.getAddress();
int txPower = (scanRecord[startByte + 24]);
if (uuid.equalsIgnoreCase(UUID)){//扫描到了
if (rssi >=lastRSS){
if (scanResultBean == null){
scanResultBean = new ScanResultBean();
}
scanResultBean.setDeviceCode(uuid+"|"+major+"|"+minor);
scanResultBean.setRss(rssi+"");
lastRSS = rssi;
}
}
}
};
static final char[] hexArray = "0123456789ABCDEF".toCharArray();
private static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
private static Timer timer = new Timer();
private static Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
// 要做的事情
super.handleMessage(msg);
if (msg.what == 1){
if (scanResultBean ==null){
scanResultBean = new ScanResultBean();
scanResultBean.setRss("");
scanResultBean.setDeviceCode("");
}
WritableMap et= Arguments.createMap();
System.out.println("===>deviceCode"+scanResultBean.getDeviceCode());
et.putString("deviceCode",scanResultBean.getDeviceCode());
et.putString("rss",scanResultBean.getRss());
BluetoothModule.sendEvent("BlueToolData",et);
resetData();
}
}
};
public static void resetData(){
lastRSS =-9999;
scanResultBean = null;
}
/**
* 开启定时器
* @param s 秒
*/
public static void startTimer(String s){
if (s == null || s.equals("")){
return;
}
if (timer ==null){
timer = new Timer();
}
timer.schedule(task, 0, Integer.parseInt(s)*1000);
}
/**
* 关闭定时器
*/
public static void stopTimer(){
if (timer !=null){
timer.cancel();
}
}
private static TimerTask task = new TimerTask() {
@Override
public void run() {
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}
};
}