SocketServerBluetooth同时接受2个以上设备

时间:2019-02-08 01:07:34

标签: android bluetooth iot android-bluetooth

我的情况如下:Android像一个主蓝牙,我有2个以上shimmer2r设备作为从设备模式,向我发送数据,例如加速度计数据。

我的Android设备处于以下模式:我的Android设备通过打开BluetoothSocketServer的BluetoothAdapter侦听RFCOMM,并且每个从属设备的Bluetooth套接字都会打开不同的BluetoothSocket。但是我想知道我是否以这种方式放置我的android设备将像TCP中的普通服务器一样工作,即我的从属设备必须连接到我的蓝牙服务器或从我的蓝牙服务器必须开始与每个从属设备的通信。 总而言之,我在类BluetoothServer中有两个公共方法:start和stop,并且有两个类似于ChatBluetooth的私有方法进行连接和连接。

class BluetoothServer private constructor() {
  /** Log tag, apps may override it.  */
  private var TAG = "BluetoothServer"
  private val NAME: String = "BLUETOOTH_SERVER"
  private val MY_UUID: UUID = UUID.fromString("a86de605-7069-41dc-96a3-83e9dabec5b3")
  private val mAdapter: BluetoothAdapter
  // Variables to check out the states of the conections
  private var currentState: Int = 0
  // Threads
  private var mAcceptThread: AcceptThread? = null
  private var mConnectThread: ConnectThread? = null
  private var mStreamThread: StreamThread? = null
  // Constants that indicate the current connection state
  private val STATE_NONE = 0       // we're doing nothing
  private val STATE_LISTEN = 1     // now listening for incoming connections
  private val STATE_CONNECTING = 2 // now initiating an outgoing connection
  private val STATE_CONNECTED = 3 // now connected to a remote device
//List of clients
  var clientList: ArrayList<BluetoothDevice>
     get() {
         return this.clientList
    }

init {
    mAdapter = BluetoothAdapter.getDefaultAdapter();
    clientList = ArrayList()
    currentState = STATE_NONE
}


private object GetInstance {
    val INSTANCE = BluetoothServer()
}

companion object {
    val instance: BluetoothServer by lazy { GetInstance.INSTANCE }
}

我的android应用程序正在侦听,但是我已经在客户端模式下创建了一个python应用程序,从这个客户端我无法到达我的srv蓝牙服务器。所以我不知道怎么了。

0 个答案:

没有答案