2222import com .shimmerresearch .driverUtilities .AssembleShimmerConfig ;
2323import com .shimmerresearch .driverUtilities .SensorDetails ;
2424import com .shimmerresearch .driverUtilities .ShimmerVerDetails .HW_ID ;
25+ import com .shimmerresearch .driverUtilities .UtilShimmer ;
2526import com .shimmerresearch .driverUtilities .ChannelDetails .CHANNEL_TYPE ;
2627import com .shimmerresearch .grpc .ShimmerBLEByteServerGrpc ;
2728import com .shimmerresearch .grpc .ShimmerBLEGRPC .BluetoothState ;
3637
3738import io .grpc .ManagedChannel ;
3839import io .grpc .ManagedChannelBuilder ;
40+ import io .grpc .StatusRuntimeException ;
3941import io .grpc .stub .StreamObserver ;
4042
4143import com .shimmerresearch .driver .BasicProcessWithCallBack ;
@@ -64,31 +66,36 @@ public class ShimmerGRPC extends ShimmerBluetooth implements Serializable{
6466 *
6567 */
6668 private static final long serialVersionUID = 5029128107276324956L ;
67-
69+
6870 public ShimmerGRPC (String macAddress , String serverHost , int serverPort ) {
6971 super ();
7072 mServerHost = serverHost ;
7173 mServerPort = serverPort ;
7274 mMacAddress = macAddress ;
75+ mMyBluetoothAddress = "" ;
7376 mUseProcessingThread = true ;
7477 if (channel ==null ) {
7578 InitializeProcess ();
7679 }
7780 }
78-
81+
82+ /**
83+ * This constructor is used for MacOS for BLE where the mFriendlyName is the BT device name (e.g. Shimmer3-6813).
84+ * BT device name is used in place of COM Port which is Classic BT only
85+ */
86+ public ShimmerGRPC (String macAddress , String mFriendlyName , String serverHost , int serverPort ) {
87+ this (mFriendlyName , serverHost , serverPort );
88+ mMyBluetoothAddress = macAddress ;
89+ mComPort = mFriendlyName ;
90+ }
91+
7992 public void InitializeProcess () {
80- // Define the server host and port
81-
82- // Create a channel to connect to the server
83- channel = ManagedChannelBuilder .forAddress (mServerHost , mServerPort )
93+ channel = ManagedChannelBuilder .forTarget (mServerHost + ":" + mServerPort )
8494 .usePlaintext () // Use plaintext communication (insecure for testing)
8595 .build ();
96+
8697 // Create a gRPC client stub
8798 blockingStub = ShimmerBLEByteServerGrpc .newBlockingStub (channel );
88-
89-
90-
91-
9299 }
93100
94101
@@ -98,8 +105,10 @@ public static void main(String[] args) {
98105
99106 JButton btnNewButton = new JButton ("Connect" );
100107
101- final ShimmerGRPC shimmer = new ShimmerGRPC ("E8EB1B713E36" ,"localhost" ,50052 );
102-
108+ final ShimmerGRPC shimmer = new ShimmerGRPC ("E8EB1B713E36" ,"localhost" ,50052 );
109+ //Use constructor below instead for MacOS, which uses the BT device name to connect, e.g. Shimmer3-6813
110+ //final ShimmerGRPC shimmer = new ShimmerGRPC("E8EB1B713E36","Shimmer3-3E36","localhost",50052);
111+
103112 SensorDataReceived sdr = shimmer .new SensorDataReceived ();
104113 sdr .setWaitForData (shimmer );
105114
@@ -271,11 +280,18 @@ protected void writeBytes(byte[] data) {
271280 // Create a request message
272281 WriteBytes request = WriteBytes .newBuilder ().setAddress (mMacAddress ).setByteToWrite (ByteString .copyFrom (data )).build ();
273282
274- // Call the remote gRPC service method
275- Reply response = blockingStub .writeBytesShimmer (request );
276-
277- // Process the response
278- System .out .println ("Received: " + response .getMessage ());
283+ try {
284+ // Call the remote gRPC service method
285+ Reply response = blockingStub .writeBytesShimmer (request );
286+
287+ // Process the response
288+ System .out .println ("Received: " + response .getMessage ());
289+ } catch (StatusRuntimeException sre ) {
290+ // Remote gRPC service is not available, so disconnect Shimmer device
291+ sre .printStackTrace ();
292+ System .out .println ("ERROR: Lost connection to GRPC Server" );
293+ connectionLost ();
294+ }
279295 }
280296
281297 @ Override
@@ -510,11 +526,15 @@ public void disconnect() throws ShimmerException {
510526 // Create a request message
511527 Request request = Request .newBuilder ().setName (mMacAddress ).build ();
512528
513- // Call the remote gRPC service method
514- Reply response = blockingStub .disconnectShimmer (request );
529+ try {
530+ // Call the remote gRPC service method
531+ Reply response = blockingStub .disconnectShimmer (request );
532+ // Process the response
533+ System .out .println ("Received: " + response .getMessage ());
534+ } catch (Exception e ) {
535+ e .printStackTrace ();
536+ }
515537
516- // Process the response
517- System .out .println ("Received: " + response .getMessage ());
518538 closeConnection ();
519539 setBluetoothRadioState (BT_STATE .DISCONNECTED );
520540 }
0 commit comments