/** * Create a listening, secure RFCOMM Bluetooth socket with Service Record. * * <p>A remote device connecting to this socket will be authenticated and communication on this * socket will be encrypted. * * <p>Use {@link BluetoothServerSocket#accept} to retrieve incoming connections from a listening * {@link BluetoothServerSocket}. * * <p>The system will assign an unused RFCOMM channel to listen on. * * <p>The system will also register a Service Discovery Protocol (SDP) record with the local SDP * server containing the specified UUID, service name, and auto-assigned channel. Remote Bluetooth * devices can use the same UUID to query our SDP server and discover which channel to connect to. * This SDP record will be removed when this socket is closed, or if this application closes * unexpectedly. * * <p>Use {@link BluetoothDevice#createRfcommSocketToServiceRecord} to connect to this socket from * another device using the same {@link UUID}. * * <p>Requires {@link android.Manifest.permission#BLUETOOTH} * * @param name service name for SDP record * @param uuid uuid for SDP record * @return a listening RFCOMM BluetoothServerSocket * @throws IOException on error, for example Bluetooth not available, or insufficient permissions, * or channel in use. */ public BluetoothServerSocket listenUsingRfcommWithServiceRecord(String name, UUID uuid) throws IOException { RfcommChannelPicker picker = new RfcommChannelPicker(uuid); BluetoothServerSocket socket; int channel; int errno; while (true) { channel = picker.nextChannel(); if (channel == -1) { throw new IOException("No available channels"); } socket = new BluetoothServerSocket(BluetoothSocket.TYPE_RFCOMM, true, true, channel); errno = socket.mSocket.bindListen(); if (errno == 0) { if (DBG) Log.d(TAG, "listening on RFCOMM channel " + channel); break; // success } else if (errno == BluetoothSocket.EADDRINUSE) { if (DBG) Log.d(TAG, "RFCOMM channel " + channel + " in use"); try { socket.close(); } catch (IOException e) { } continue; // try another channel } else { try { socket.close(); } catch (IOException e) { } socket.mSocket.throwErrnoNative(errno); // Exception as a result of bindListen() } } int handle = -1; try { handle = mService.addRfcommServiceRecord(name, new ParcelUuid(uuid), channel, new Binder()); } catch (RemoteException e) { Log.e(TAG, "", e); } if (handle == -1) { try { socket.close(); } catch (IOException e) { } throw new IOException("Not able to register SDP record for " + name); } socket.setCloseHandler(mHandler, handle); return socket; }
public void run(){ BluetoothSocket receivedSocket = null; while(true){ try{ //クライアント側からの接続要求待ち。ソケットが返される。 receivedSocket = servSock.accept(); }catch(IOException e){ break; } if(receivedSocket != null){ //ソケットを受け取れていた(接続完了時)の処理 //RwClassにmanageSocketを移す ReadWriteModel rw = new ReadWriteModel(mContext, receivedSocket, myNumber); rw.start(); try { //処理が完了したソケットは閉じる。 servSock.close(); } catch (IOException e) { e.printStackTrace(); } break; public class BluetoothClientThread extends Thread { //クライアント側の処理 private final BluetoothSocket clientSocket; private final BluetoothDevice mDevice; private Context mContext; //UUIDの生成 public static final UUID TECHBOOSTER_BTSAMPLE_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); static BluetoothAdapter myClientAdapter; public String myNumber; //コンストラクタ定義 public BluetoothClientThread(Context context, String myNum , BluetoothDevice device, BluetoothAdapter btAdapter){ //各種初期化 mContext = context; BluetoothSocket tmpSock = null; mDevice = device; myClientAdapter = btAdapter; myNumber = myNum; try{ //自デバイスのBluetoothクライアントソケットの取得 tmpSock = device.createRfcommSocketToServiceRecord(TECHBOOSTER_BTSAMPLE_UUID); }catch(IOException e){ e.printStackTrace(); } clientSocket = tmpSock; } public void run(){ //接続要求を出す前に、検索処理を中断する。 if
/** * Construct a SCO server socket. Call #accept to retrieve connections to this socket. * * @return A SCO BluetoothServerSocket * @throws IOException On error, for example Bluetooth not available, or insufficient permissions. * @hide */ public static BluetoothServerSocket listenUsingScoOn() throws IOException { BluetoothServerSocket socket = new BluetoothServerSocket(BluetoothSocket.TYPE_SCO, false, false, -1); int errno = socket.mSocket.bindListen(); if (errno != 0) { try { socket.close(); } catch (IOException e) { } socket.mSocket.throwErrnoNative(errno); } return socket; }
/** * Construct an unencrypted, unauthenticated, RFCOMM server socket. Call #accept to retrieve * connections to this socket. * * @return An RFCOMM BluetoothServerSocket * @throws IOException On error, for example Bluetooth not available, or insufficient permissions. * @hide */ public BluetoothServerSocket listenUsingInsecureRfcommOn(int port) throws IOException { BluetoothServerSocket socket = new BluetoothServerSocket(BluetoothSocket.TYPE_RFCOMM, false, false, port); int errno = socket.mSocket.bindListen(); if (errno != 0) { try { socket.close(); } catch (IOException e) { } socket.mSocket.throwErrnoNative(errno); } return socket; }
/** * Create a listening, secure RFCOMM Bluetooth socket. * * <p>A remote device connecting to this socket will be authenticated and communication on this * socket will be encrypted. * * <p>Use {@link BluetoothServerSocket#accept} to retrieve incoming connections from a listening * {@link BluetoothServerSocket}. * * <p>Valid RFCOMM channels are in range 1 to 30. * * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} * * @param channel RFCOMM channel to listen on * @return a listening RFCOMM BluetoothServerSocket * @throws IOException on error, for example Bluetooth not available, or insufficient permissions, * or channel in use. * @hide */ public BluetoothServerSocket listenUsingRfcommOn(int channel) throws IOException { BluetoothServerSocket socket = new BluetoothServerSocket(BluetoothSocket.TYPE_RFCOMM, true, true, channel); int errno = socket.mSocket.bindListen(); if (errno != 0) { try { socket.close(); } catch (IOException e) { } socket.mSocket.throwErrnoNative(errno); } return socket; }