📄 bluetoothtcpclient.java
字号:
case HCI_EVENT_COMMAND_STATUS:
Debug.println(6, "BluetoothTCPClient: Received HCI Command Status Event:", packet);
receive_HCI_Event_Command_Status(packet);
break;
default:
System.err.println("BluetoothTCPClient: Received Unknown HCI Event Packet:" +
Debug.printByteArray(packet));
}
break;
case(byte)L2CAP_CREATE_CONNECTION_RESPONSE: {
Debug.println(6, "BluetoothTCPClient: Received L2CAP Create Connection Response:", packet);
receive_L2CAP_Create_Connection_Response(packet);
break;
}
case(byte)L2CAP_DISCONNECT_CHANNEL_REQUEST: {
Debug.println(6, "BluetoothTCPClient: Received L2CAP Disconnect Channel Request:", packet);
receive_L2CAP_Disconnect_Channel_Request(packet);
break;
}
case(byte)L2CAP_PACKET: {
Debug.println(6, "BluetoothTCPClient: Received L2CAP Packet:", packet);
receive_L2CAP_Packet(packet);
break;
}
/*case PACKET_TYPE_ACL :
//Debug.println("HCITransport: Received Data Packet:" + Debug.printByteArray(hciPacket));
//receive_HCI_Data_Packet(packet);
break; */
default:
System.err.println("BluetoothTCPClient: Received Packet of unknown Type: " + Debug.printByteArray(packet));
}
}
/**
* Parses Command Complete Event Packet and makes the Command Response available to the
* <code>send_HCI_Command_Packet(byte[])</code> method.
*/
private synchronized void receive_HCI_Event_Command_Complete(byte[] packetData) {
short opCode = (short)((packetData[5] << 8) | (packetData[4] & 0xff));
//Debug.println("Command Complete Event: " + opCode);
while (commandResponse != null) {
try { this.wait(100); }
catch (InterruptedException e) { }
}
if (opCode == commandResponseOpCode) { commandResponse = packetData; }
}
/**
* Parses Command Status Event Packet and makes the Command Response available to the
* <code>send_HCI_Command_Packet(byte[])</code> method.
*/
private synchronized void receive_HCI_Event_Command_Status(byte[] packetData) {
short opCode = (short)((packetData[6] << 8) | (packetData[5] & 0xff));
//Debug.println("Command Status Event: " + opCode);
while (commandResponse != null) {
try { this.wait(100); }
catch (InterruptedException e) { }
}
if (opCode == commandResponseOpCode) { commandResponse = packetData; }
}
private void receive_L2CAP_Create_Connection_Response(byte[] packet) {
short channelHandel = (short)((((short)packet[3]) & 0xff) | (((short)packet[4]) & 0xff) << 8);
byte channelState = packet[5];
short localCID = (short)((((short)packet[6]) & 0xff) | (((short)packet[7]) & 0xff) << 8);
short remoteCID = (short)((((short)packet[8]) & 0xff) | (((short)packet[9]) & 0xff) << 8);
L2CAPChannel channel = channels[channelHandel];
if (channel != null) {
channel.channelState = channelState;
channel.localChannelID = localCID;
channel.remoteChannelID = remoteCID;
}
}
private void receive_L2CAP_Disconnect_Channel_Request(byte[] packet) {
short channelHandel = (short)((((short)packet[3]) & 0xff) | (((short)packet[4]) & 0xff) << 8);
L2CAPChannel channel = channels[channelHandel];
channels[channelHandel] = null;
if (channel != null) {
channel.channelState = L2CAPChannel.CLOSED;
channel.wasDisconnected();
}
}
private void receive_L2CAP_Packet(byte[] packet) {
short channelHandel = (short)((((short)packet[3]) & 0xff) | (((short)packet[4]) & 0xff) << 8);
int length = ((packet[1] & 0xff) | (packet[2] & 0xff) << 8) - 2;
L2CAPChannel channel = channels[channelHandel];
if (channel != null) {
byte[] l2capPacket = new byte[length];
System.arraycopy(packet, 5, l2capPacket, 0, l2capPacket.length);
channel.receiveL2CAPPacket(l2capPacket);
}
}
/**
* @see org.javabluetooth.stack.BluetoothStack#connectL2CAPChannel(org.javabluetooth.stack.l2cap.L2CAPChannel,
* javax.bluetooth.RemoteDevice, short)
*/
public void connectL2CAPChannel(L2CAPChannel channel, RemoteDevice remoteDevice, short psm) throws HCIException {
if (!isConnected) throw new HCIException("BluetoothTCPClient is not connected.");
short channelHandle = -1;
for (int i = 0; i < channels.length; i++) {
if (channels[i] == null) {
channels[i] = channel;
channel.l2capSender = this;
channel.remoteAddress = remoteDevice.bdAddrLong;
channelHandle = (short)i;
break;
}
}
if (channelHandle == -1) throw new HCIException("Connect L2CAPChannel failed. No Open Channel Slots.");
byte[] createL2CAPChannelRequest = {
L2CAP_CREATE_CONNECTION_REQUEST, 0x0e, 0x00, //fixed length 14
(byte)((channelHandle) & 0xff), (byte)((channelHandle >> 8) & 0xff), (byte)((psm) & 0xff), (byte)((psm >> 8) & 0xff),
(byte)((remoteDevice.bdAddrLong) & 0xff), (byte)((remoteDevice.bdAddrLong >> 8) & 0xff),
(byte)((remoteDevice.bdAddrLong >> 16) & 0xff), (byte)((remoteDevice.bdAddrLong >> 24) & 0xff),
(byte)((remoteDevice.bdAddrLong >> 32) & 0xff), (byte)((remoteDevice.bdAddrLong >> 40) & 0xff),
remoteDevice.pageScanRepMode, remoteDevice.pageScanMode, (byte)((remoteDevice.clockOffset) & 0xff),
(byte)((remoteDevice.clockOffset >> 8) & 0xff)
};
try {
Debug.println(6, "BluetoothTCPClient: Sending L2CAP Create Connection Request:", createL2CAPChannelRequest);
socketOut.write(createL2CAPChannelRequest);
socketOut.flush();
}
catch (IOException e) {
cleanExit();
throw new HCIException("IOException: " + e);
}
int timeout = 0;
while (channel.channelState == L2CAPChannel.CLOSED) {
try {
Thread.sleep(1000);
timeout++;
}
catch (InterruptedException e) { }
if (timeout == 100) throw new HCIException("Connect L2CAPChannel timed out.");
}
if (channel.channelState == L2CAPChannel.FAILED) throw new HCIException("Connect L2CAPChannel failed.");
}
/** @see org.javabluetooth.stack.l2cap.L2CAPSender#sendL2CAPPacket(long, short, byte[]) */
public void sendL2CAPPacket(L2CAPChannel channel, byte[] packet) throws IOException {
byte channelHandel = -1;
for (int i = 0; i < channels.length; i++) { if (channels[i] == channel) channelHandel = (byte)i; }
if (channelHandel == -1) throw new IOException("Unable to send to channel.");
byte[] l2capPacket = new byte[5 + packet.length];
l2capPacket[0] = L2CAP_PACKET;
l2capPacket[1] = (byte)((packet.length + 2) & 0xff);
l2capPacket[2] = (byte)((packet.length + 2 >> 8) & 0xff);
l2capPacket[3] = (byte)((channelHandel) & 0xff);
l2capPacket[4] = (byte)((channelHandel >> 8) & 0xff);
System.arraycopy(packet, 0, l2capPacket, 5, packet.length);
try {
Debug.println(6, "BluetoothTCPClient: Sending L2CAP Packet:", l2capPacket);
socketOut.write(l2capPacket);
socketOut.flush();
}
catch (IOException e) {
cleanExit();
throw e;
}
}
/** @see org.javabluetooth.stack.l2cap.L2CAPSender#closeL2CAPChannel(org.javabluetooth.stack.l2cap.L2CAPChannel) */
public void closeL2CAPChannel(L2CAPChannel channel) {
byte channelHandel = -1;
for (int i = 0; i < channels.length; i++) { if (channels[i] == channel) channelHandel = (byte)i; }
if (channelHandel != -1) {
byte[] l2capPacket = {
L2CAP_DISCONNECT_CHANNEL_REQUEST, 0x02, 0x00, (byte)((channelHandel) & 0xff),
(byte)((channelHandel >> 8) & 0xff)
};
try {
Debug.println(6, "BluetoothTCPClient: Sending L2CAP Disconnect Channel Request:", l2capPacket);
socketOut.write(l2capPacket);
socketOut.flush();
}
catch (IOException e) { cleanExit(); }
}
}
/**
* @see org.javabluetooth.stack.BluetoothStack#openL2CAPService(org.javabluetooth.stack.l2cap.L2CAPChannel, short,
* short, byte[])
*/
public void registerL2CAPService(L2CAPChannel channel, short serviceUUID, short browseUUID,
byte[] serviceRecord) throws HCIException {
if (!isConnected) throw new HCIException("BluetoothTCPClient is not connected.");
short channelHandle = -1;
for (int i = 0; i < channels.length; i++) {
if (channels[i] == null) {
channels[i] = channel;
channel.l2capSender = this;
channelHandle = (short)i;
break;
}
}
if (channelHandle == -1) throw new HCIException("Register SDP Service failed. No Open Channel Slots.");
short length = (short)(6 + serviceRecord.length);
byte[] registerL2CAPServiceRequest = new byte[3 + length];
registerL2CAPServiceRequest[0] = SDP_REGISTER_SERVICE_REQUEST;
registerL2CAPServiceRequest[1] = (byte)((length) & 0xff);
registerL2CAPServiceRequest[2] = (byte)((length >> 8) & 0xff);
registerL2CAPServiceRequest[3] = (byte)((channelHandle) & 0xff);
registerL2CAPServiceRequest[4] = (byte)((channelHandle >> 8) & 0xff);
registerL2CAPServiceRequest[5] = (byte)((serviceUUID) & 0xff);
registerL2CAPServiceRequest[6] = (byte)((serviceUUID >> 8) & 0xff);
registerL2CAPServiceRequest[7] = (byte)((browseUUID) & 0xff);
registerL2CAPServiceRequest[8] = (byte)((browseUUID >> 8) & 0xff);
System.arraycopy(serviceRecord, 0, registerL2CAPServiceRequest, 9, serviceRecord.length);
try {
Debug.println(6, "BluetoothTCPClient: Sending SDP Register Service Request:", registerL2CAPServiceRequest);
socketOut.write(registerL2CAPServiceRequest);
socketOut.flush();
}
catch (IOException e) {
cleanExit();
throw new HCIException("IOException: " + e);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -