Hardware Reference
In-Depth Information
then be able to click on the device she wants to connect to. In order to avoid the GUI
complexities, we're going to simplify the use case a bit by making assumptions.
We'll assume we want to connect to a SensorTag device. The following code checks for
advertising packets with the device name “SensorTag” and automatically initiates a
connection request to any device with that name:
@Override
public
void
uiDeviceFound
(
final
BluetoothDevice
device
,
final
int
rssi
,
final
byte
[]
record
)
{
String
msg
=
"uiDeviceFound: "
+
device
.
getName
()+
", "
+
rssi
+
", "
+
\
rssi
.
toString
();
Log
.
d
(
"DEBUG"
,
"uiDeviceFound: "
+
msg
);
if
(
device
.
getName
().
equals
(
"SensorTag"
)
==
true
)
{
bool
status
;
status
=
mBleWrapper
.
connect
(
device
.
getAddress
().
toString
());
if
(
status
==
false
)
{
Log
.
d
(
"DEBUG"
,
"uiDeviceFound: Connection problem"
);
}
}
}
This automatic connection process avoids going into some Android GUI complexities
and lets us focus on the BLE library we're working with.
The previous callback code for
uiDeviceFound()
has been modified to remove the
popup messagebox. After finding a device with a name equal to “SensorTag,” the code
instructs the
BleWrapper
to connect to the device and pass in the device's address (in‐
troduced in
“Bluetooth Device Address” on page 19
) in string format. The
BleWrap
per
will use the address to send a unicast connection request to the device. It will then
inform you via the
uiDeviceConnected()
callback if the connection was successful.
You will need to override this callback and add handler code. If the connection was not
successful for some reason, the status of the method call will return false. You can use
this to log a message to the
logcat
console for debugging.
Communicating with a Remote Device
Once you're successfully connected to a remote device, the
BleWrapper
will automati‐
cally start service discovery (as described in
“Service and Characteristic Discovery” on
page 69
) for the new device. This means that it will request the new device to list all the
services and characteristics from the device and store them in a list.