Using MeshService

Since MeshService is the core class of the CSRMesh Android library, you will need it in your main activity. Create a global variable and bind it inside the onCreate method:

private MeshService mMeshService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Make a connection to MeshService to enable us to use its services.
Intent bindIntent = new Intent(this, MeshService.class);
bindService(bindIntent, mServiceConnection, Context.BIND_AUTO_CREATE);
}

If you notice, there is an unknown variable in the previous code, mServiceConnection. This is an object that will handle the service connection. Declare it using the following code:

private ServiceConnection mServiceConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder rawBinder) {
mService = ((MeshService.LocalBinder) rawBinder).getService();
}

public void onServiceDisconnected(ComponentName classname) {
mService = null;
}
};

MeshService is now ready, but the CSRMesh stack has not yet been initialized. We will first declare a handler that is responsible for receiving messages from CSRMesh (which can later be used in the user interface of the application). In order to do this, set a mMeshHandler in mMeshService:

private final Handler mMeshHandler = new Handler(this);

private boolean connect(){
mMeshService.setHandler(mMeshHandler);
}

The next step is to start a Bluetooth scan to detect CSRMesh devices in the vicinity. For this, we need to declare a ScanCallBack object, which will be called as CSRMesh library hears the scan response:

private LeScanCallback mScanCallBack = new LeScanCallback() {
@Override
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
mMeshService.processMeshAdvert(device, scanRecord, rssi);
}
};

private boolean connect(){
mMeshService.setHandler(mMeshHandler);
mMeshService.setLeScanCallback(mScanCallBack);
}

This mScanCallback function must explicitly call mMeshService.processMeshAdvert() for every advertisement it receives. This will allow the library to consume any CSRMesh advertisement packets. The library is smart enough to automatically enable and disable Bluetooth scanning according to your requirements. But if continuous scanning is required, you can do it by calling this method: 

mMeshService.setContinuousLeScanEnabled(true);

By default, BLE notifications are enabled on the bridge, but you can manually enable it by calling this method:

setBridgeNotificationsEnabled(true);

After scanning, you can connect the library to the bridge by calling the following:

mMeshService.connectBridge(bridgeDevice);

The bridgeDevice variable is the BluetoothDevice object that represents a bridge device. Scanning for a bridge device is the same as scanning for BLE devices. Since you want to filter only CSRMesh devices in your scan, you can use the bridge service UUID, 0xFEF1.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.67.166