public void sendMessage(String destination, String message) { try { BluetoothSocket myBsock = btSockets.get(destination); if (myBsock != null) { OutputStream outStream = myBsock.getOutputStream(); byte[] stringAsBytes = (message + " ").getBytes(); stringAsBytes[stringAsBytes.length - 1] = 0; // Add a stop marker outStream.write(stringAsBytes); DebugLog.i(TAG, "successful sendMessage - Dest:" + destination + ", Msg:" + message); Message msg = mHandler.obtainMessage(DynamicAppBluetooth.MESSAGE_WRITE); Bundle bundle = new Bundle(); bundle.putBoolean(DynamicAppBluetooth.SEND_RESULT, true); msg.setData(bundle); mHandler.sendMessage(msg); } else { Message msg = mHandler.obtainMessage(DynamicAppBluetooth.MESSAGE_WRITE); Bundle bundle = new Bundle(); bundle.putBoolean(DynamicAppBluetooth.SEND_RESULT, false); msg.setData(bundle); mHandler.sendMessage(msg); } } catch (IOException e) { DebugLog.e(TAG, "IOException in sendMessage - Dest:" + destination + ", Msg:" + message); Message msg = mHandler.obtainMessage(DynamicAppBluetooth.MESSAGE_WRITE); Bundle bundle = new Bundle(); bundle.putBoolean(DynamicAppBluetooth.SEND_RESULT, false); msg.setData(bundle); mHandler.sendMessage(msg); } return; }
public synchronized void start() { DebugLog.i(TAG, "start"); setState(STATE_LISTEN); if (mSecureAcceptThread == null) { DebugLog.i(TAG, "starting accept thread..."); mSecureAcceptThread = new AcceptThread(); mSecureAcceptThread.start(); } }
/** This function hides the notification bar */ public static void hideStatusBar() { try { new Thread( new Runnable() { @Override public void run() { if (statusBarIsShown) { dynamicApp.runOnUiThread( new Runnable() { public void run() { statusBarIsShown = false; dynamicApp .getWindow() .setFlags( WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); dynamicApp.getWebView().getLayoutParams().height = RelativeLayout.LayoutParams.MATCH_PARENT; } }); } } }) .start(); } catch (Exception ex) { DebugLog.e(TAG, "error:" + ex); } }
/** * Write to the ConnectedThread in an unsynchronized manner * * @param out The bytes to write * @see ConnectedThread#write(byte[]) */ public void write(BluetoothDevice device, byte[] out) { // Create temporary object ConnectedThread r = null; // Synchronize a copy of the ConnectedThread synchronized (this) { String addressToStop = device.getAddress(); int size = connectedThreads.size(); for (int i = 0; i < size; i++) { ConnectedThread conThread = connectedThreads.get(i); if (conThread != null) { String address = conThread.getMmDevice().getAddress(); if (addressToStop.equals(address)) { r = conThread; break; } } } } // Perform the write unsynchronized if (r != null) { r.write(out); } else { DebugLog.i(TAG, "write thread not connected"); } }
public synchronized void disconnect(BluetoothDevice device) { String addressToStop = device.getAddress(); BluetoothSocket socket = btSockets.get(addressToStop); if (socket != null) { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } btSockets.remove(addressToStop); btDeviceAddresses.remove(addressToStop); DebugLog.i(TAG, "stop"); int size = connectedThreads.size(); for (int i = 0; i < size; i++) { ConnectedThread conThread = connectedThreads.get(i); if (conThread != null) { String address = conThread.getMmDevice().getAddress(); if (addressToStop.equals(address)) { conThread = null; connectedThreads.remove(i); break; } } } this.start(); }
public synchronized void connect(BluetoothDevice device) { DebugLog.i(TAG, "connect to: " + device); mConnectThread = new ConnectThread(device); mConnectThread.start(); setState(STATE_CONNECTING); }
public ConnectedThread(BluetoothDevice device, BluetoothSocket socket) { // BluetoothSocket bSock = btSockets.get(socke); mmDevice = device; DebugLog.i(TAG, "create ConnectedThread: "); InputStream tmpIn = null; OutputStream tmpOut = null; // Get the BluetoothSocket input and output streams try { tmpIn = socket.getInputStream(); tmpOut = socket.getOutputStream(); } catch (IOException e) { DebugLog.e(TAG, "temp sockets not created"); } mmInStream = tmpIn; mmOutStream = tmpOut; }
public void run() { mAdapter.cancelDiscovery(); try { DebugLog.i(TAG, "start of AcceptThread:"); for (int i = 0; (i < MAX_CONNECTIONS) && (connections < MAX_CONNECTIONS); ) { String uid = uuidList.get(i); if (availableUuids.get(uid)) { BluetoothServerSocket myServerSocket = mAdapter.listenUsingRfcommWithServiceRecord(NAME_SECURE, UUID.fromString(uid)); BluetoothSocket socket = myServerSocket.accept(); if (socket != null) { synchronized (BluetoothConnectionService.this) { DebugLog.i(TAG, "AcceptThread:" + i); String address = socket.getRemoteDevice().getAddress(); btSockets.put(address, socket); connections++; availableUuids.put(uid, false); deviceUuidList.put(socket.getRemoteDevice(), uid); connected(socket, socket.getRemoteDevice()); try { myServerSocket.close(); } catch (IOException e) { DebugLog.e(TAG, "Could not close unwanted socket"); } } } } else { DebugLog.e(TAG, "try AcceptThread:" + i); } i++; if ((i == MAX_CONNECTIONS) && (connections < MAX_CONNECTIONS)) { i = 0; } } DebugLog.i(TAG, "end of AcceptThread:"); connectionFullFailure(); } catch (IOException e) { } }
/** * Write to the connected OutStream. * * @param buffer The bytes to write */ public void write(byte[] buffer) { try { mmOutStream.write(buffer); // Share the sent message back to the UI Activity mHandler.obtainMessage(DynamicAppBluetooth.MESSAGE_WRITE, -1, -1, buffer).sendToTarget(); } catch (IOException e) { DebugLog.e(TAG, "Exception during write"); } }
private BluetoothSocket getConnectedSocket(BluetoothDevice myBtServer, UUID uuidToTry) { BluetoothSocket myBSock; try { myBSock = myBtServer.createRfcommSocketToServiceRecord(uuidToTry); myBSock.connect(); return myBSock; } catch (IOException e) { DebugLog.e(TAG, "IOException in getConnectedSocket"); } return null; }
public void run() { DebugLog.i(TAG, "BEGIN mConnectedThread"); final int MAX_SIZE = 5242880; byte[] buffer = new byte[MAX_SIZE]; int bytesRead = -1; String message = ""; // Keep listening to the InputStream while connected while (true) { try { message = ""; bytesRead = mmInStream.read(buffer); if (bytesRead != -1) { while ((bytesRead >= 0) && (buffer[bytesRead - 1] != 0)) { message = message + new String(buffer, 0, bytesRead); bytesRead = mmInStream.read(buffer); } if (buffer[bytesRead - 1] == 0) { message = message + new String(buffer, 0, bytesRead - 1); // Remove stop marker Message msg = mHandler.obtainMessage(DynamicAppBluetooth.MESSAGE_READ); Bundle bundle = new Bundle(); bundle.putBoolean(DynamicAppBluetooth.READ_RESULT, true); bundle.putString(DynamicAppBluetooth.RECEIVED_MESSAGE, message); bundle.putString(DynamicAppBluetooth.DEVICE_NAME, mmDevice.getName()); bundle.putString(DynamicAppBluetooth.DEVICE_ADD, mmDevice.getAddress()); msg.setData(bundle); mHandler.sendMessage(msg); } } else { DebugLog.e(TAG, "end of read"); } } catch (IOException e) { DebugLog.e(TAG, "disconnected"); connectionLost(mmDevice); // Start the service over to restart listening mode BluetoothConnectionService.this.start(); break; } } }
@Override public void execute() { DebugLog.i(TAG, "parameters are: " + params); if (methodName.equalsIgnoreCase("notify")) { dynamicApp.callJsEvent(PROCESSING_FALSE); this.customNotify(); } else { dynamicApp.callJsEvent(PROCESSING_FALSE); this.cancelNotification(); } }
public void disconnectAll() { try { for (int i = 0; i < btDeviceAddresses.size(); i++) { BluetoothSocket socket = btSockets.get(btDeviceAddresses.get(i)); if (socket != null) socket.close(); } btSockets = new HashMap<String, BluetoothSocket>(); connectedThreads = new ArrayList<ConnectedThread>(); btDeviceAddresses = new ArrayList<String>(); } catch (IOException e) { DebugLog.e(TAG, "IOException in shutdown"); } }
/** Stop all threads */ public synchronized void stop() { DebugLog.i(TAG, "stop"); if (mConnectThread != null) { mConnectThread.cancel(); mConnectThread = null; } if (mSecureAcceptThread != null) { mSecureAcceptThread = null; } this.disconnectAll(); setState(STATE_NONE); }
/** This function shows the notification bar when there is a new notification */ private static void showStatusBar() { try { new Thread( new Runnable() { public void run() { if (!statusBarIsShown) { dynamicApp.runOnUiThread( new Runnable() { public void run() { statusBarIsShown = true; dynamicApp .getWindow() .clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); } }); } } }) .start(); } catch (Exception ex) { DebugLog.e(TAG, "error:" + ex); } }
public void onCharacteristicChanged(BluetoothDevice d, BleCharacteristic characteristic) { DebugLog.w(TAG, "onCharacteristicChanged"); }
public void onSetCharacteristicAuthRequirement( BluetoothDevice d, BleCharacteristic characteristic, int instanceID) { DebugLog.w(TAG, "onSetCharacteristicAuthRequirement"); }
public void onReadCharacteristicComplete(BluetoothDevice d, BleCharacteristic characteristic) { DebugLog.w(TAG, "refreshOneCharacteristicComplete"); }
public void onRefreshComplete(BluetoothDevice d) { DebugLog.w(TAG, "onRefreshComplete"); }
public void characteristicsRetrieved(BluetoothDevice d) { DebugLog.w(TAG, "characteristicsRetrieved"); }
public void onWriteCharacteristicComplete( int status, BluetoothDevice d, BleCharacteristic characteristic) { DebugLog.w(TAG, "onWriteCharacteristicComplete"); }
public void connectionFullFailure() { // this.setState(STATE_FULL); DebugLog.i(TAG, "Sockets are full."); }
private synchronized void setState(int state) { DebugLog.i(TAG, "setState() " + mState + " -> " + state); mState = state; mHandler.obtainMessage(DynamicAppBluetooth.MESSAGE_STATE_CHANGE, state, -1).sendToTarget(); }