-
Notifications
You must be signed in to change notification settings - Fork 3
/
IBeacon.scala
177 lines (149 loc) · 5.24 KB
/
IBeacon.scala
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
package org.shokai.ibeacon;
import android.app.Activity;
import android.content.Context;
import android.bluetooth.{BluetoothManager, BluetoothAdapter, BluetoothDevice};
object IBeacon{
val VERSION = "0.2.2"
}
class IBeacon(context:Context) extends EventEmitter{
import scala.collection.mutable.Map;
import scala.collection.immutable.Range
val beacons = Map.empty[String, Beacon]
val bluetoothManager:BluetoothManager =
context.getSystemService(Context.BLUETOOTH_SERVICE).asInstanceOf[BluetoothManager]
val bluetoothAdapter:BluetoothAdapter = bluetoothManager.getAdapter()
bluetoothAdapter.startLeScan(new BluetoothAdapter.LeScanCallback(){
override def onLeScan(device:BluetoothDevice, rssi:Int, scanRecord:Array[Byte]){
val beacon:Beacon = new Beacon(rssi, scanRecord)
if(beacon.error != null) return
emit("beacon", beacon)
if(!beacons.contains(beacon.name) ||
beacon.createAt - beacons(beacon.name).createAt > 5000){
emit("discover", beacon) // new beacon
}
beacons(beacon.name) = beacon
}
})
def onBeacon(callback:(Beacon) => Unit):Int = {
on("beacon", (beacon) => {
callback(beacon.asInstanceOf[Beacon])
})
}
def onDiscover(callback:(Beacon) => Unit):Int = {
on("discover", (beacon) => {
callback(beacon.asInstanceOf[Beacon])
})
}
def onDiscover(uuid:String, major:String, minor:String, callback:(Beacon) => Unit):Int = {
on("discover", (_beacon) => {
val beacon = _beacon.asInstanceOf[Beacon]
if((uuid == null || beacon.uuid.replace("-","").equals(uuid.replace("-","").toUpperCase())) &&
(major == null || beacon.major.toUpperCase().equals(major.toUpperCase())) &&
(minor == null || beacon.minor.toUpperCase().equals(minor.toUpperCase()))){
callback(beacon)
}
})
}
def onRegion(uuid:String, major:String, minor:String, range:Range, callback:(Beacon) => Unit):Int = {
val accuracy = 30
val rssis = scala.collection.mutable.Queue.empty[Int]
var last:Long = 0
on("beacon", (_beacon) => {
val beacon = _beacon.asInstanceOf[Beacon]
if((uuid == null || beacon.uuid.replace("-","").equals(uuid.replace("-","").toUpperCase())) &&
(major == null || beacon.major.toUpperCase().equals(major.toUpperCase())) &&
(minor == null || beacon.minor.toUpperCase().equals(minor.toUpperCase())) ){
rssis.enqueue(beacon.rssi)
if(rssis.size > accuracy){
rssis.dequeue()
beacon.rssi = rssis.reduce((a,b) => a+b)/rssis.size // average
if(range.min < beacon.rssi && beacon.rssi < range.max){
if(System.currentTimeMillis() - last > 5000){
callback(beacon)
}
last = System.currentTimeMillis()
}
}
}
})
}
def onFar(uuid:String, major:String, minor:String, callback:(Beacon) => Unit):Int = {
onRegion(uuid, major, minor, Range(-90,-60), callback)
}
def onNear(uuid:String, major:String, minor:String, callback:(Beacon) => Unit):Int = {
onRegion(uuid, major, minor, Range(-60,-20), callback)
}
def onImmediate(uuid:String, major:String, minor:String, callback:(Beacon) => Unit):Int = {
onRegion(uuid, major, minor, Range(-20,0), callback)
}
}
class Beacon(_rssi:Int, scanRecord:Array[Byte]){
var rssi = _rssi
var major:String = null
var minor:String = null
var uuid:String = null
var error:String = "not init"
val createAt:Long = System.currentTimeMillis()
if(scanRecord.length < 30){
error = "not iBeacn (length < 30)"
}
else if(scanRecord(5) != 0x4c.toByte ||
scanRecord(6) != 0x00.toByte ||
scanRecord(7) != 0x02.toByte ||
scanRecord(8) != 0x15.toByte){
error = "not iBeacon (format error)"
}
else{
uuid = Array(9.to(12), 13.to(14), 15.to(16), 17.to(18), 19.to(24)).map(
range =>
range.map(i => "%02x".format(scanRecord(i))).mkString.toUpperCase
).mkString("-")
major = 25.to(26).map(i => "%02x".format(scanRecord(i)).toUpperCase ).mkString
minor = 27.to(28).map(i => "%02x".format(scanRecord(i)).toUpperCase ).mkString
error = null
}
override def toString:String = {
s"UUID=${uuid} Major=${major} Minor=${minor} RSSI=${rssi}"
}
def name:String = {
return s"${uuid}.${major}.${minor}"
}
}
trait EventEmitter{
val List = scala.collection.mutable.LinkedList
var eid = 0
class Event(_id:Int, _name:String, _callback:(Any) => Unit, _once:Boolean){
var name = _name
var callback = _callback
val id = _id
var once = _once
}
var __events = List.empty[Event]
def on(name:String, callback:(Any) => Unit):Int = {
eid += 1
__events = __events :+ new Event(eid, name, callback, false)
return eid
}
def once(name:String, callback:(Any) => Unit):Int = {
eid += 1
__events = __events :+ new Event(eid, name, callback, true)
return eid
}
def emit(name:String, args:Any){
var once_ids = List.empty[Int]
__events.foreach( (e) => {
if(e.name.equals(name)){
e.callback(args)
if(e.once) once_ids = once_ids :+ e.id
}
})
__events = __events.filter((e) => {
!once_ids.contains(e.id)
})
}
def removeListener(id:Int){
__events = __events.filter((e) => {
e.id != id
})
}
}