得意犹堪夸世俗,诏黄新湿字如鸦。这篇文章主要讲述在Android 2.3上实现OBEX PUSH服务器相关的知识,希望能为你提供帮助。
我需要在android 2.3设备上设置应用内OBEX服务器。使用蓝牙聊天示例代码我能够设置OBEX服务器。但是,服务器需要使用自定义UUID,因此该服务未注册为“OBEX服务器”
# sdptool browse local
...(snip)...
Service Name: OBEX Object Push
Service RecHandle: 0x10000
Service Class ID List:
UUID 128: ab123abc-1a2b-3c4d-5d7f-1234567890ab
Protocol Descriptor List:
"L2CAP" (0x0100)
"RFCOMM" (0x0003)
Channel: 18
因此,当我收到数据时,看起来我收到的是原始OBEX连接请求:
80 00 07 10 00 04 00 00 00 00 ...(snip)... 00 00 00 (1kb file)
是否有我可以使用的OBEX实现,或者我是否必须自己实现协议?
我不想使用内置的OBEX服务器 - 这必须在应用程序中。我尝试过BlueCove,但是在注册服务时遇到问题我放弃了它。
是的,我确实看到了this帖子,并在其中阅读了link,但通过高兴,必须有一个更简单的方法!
答案我自己最终实现了协议。它没有我想象的那么毛茸茸。由于我只希望连接一个特定的客户端,而且我知道客户端只会推送一个文件,所以我只能部分地实现我需要的协议部分。
一些有助于理解OBEX协议的文档是Obex13.pdf:http://gitorious.org/gammu/gsm-docs/trees/e5161a75fb1e1c1608959b27ae3c3940bcf0911b/standards/obex
我如何解析套接字输入流的快速片段:(注意OBEXUtils和OBEXConstants是我的类。)
try
{
//Read all bytes passed in
bytes = mmInStream.read(buffer);
//Ensure we have the entire packet before we proceed
// Packet length is in the 1st and 2nd byte
expectedLength = OBEXUtils.bytesToShort(buffer[OBEXConstant.LENGTH_IDENTIFIER],
buffer[OBEXConstant.LENGTH_IDENTIFIER + 1]);
packetLength = bytes;
//Keep reading until we get what we expect.
while (packetLength <
expectedLength)
{
bytes = mmInStream.read(buffer, packetLength, maxPacketSize);
packetLength += bytes;
}//Switch on Packet Header
switch (buffer[OBEXConstant.HEADER_IDENTIFIER])
{
case OBEXConstant.CONNECT:
//Parse the packet and return an acknowledgement packet
write(OBEXConnect.parsePacket(buffer));
break;
case OBEXConstant.PUT:
case OBEXConstant.PUT_FINAL:
//Parse the PUT packet and return an acknowledgement packet
//For Parsing PUT packets I referred to the android and bluecove implementations
write(putPacket.appendPacket(buffer, packetLength));
break;
case OBEXConstant.DISCONNECT:
//Parse the packet and return an acknowledgement packet
write(OBEXDisconnect.parsePacket(buffer));
break;
case OBEXConstant.GET:
case OBEXConstant.GET_FINAL:
case OBEXConstant.SETPATH:
case OBEXConstant.SETPATH_FINAL:
case OBEXConstant.SESSION:
//Did not implement these
break;
case OBEXConstant.ABORT:
Log.w(Constant.TAG, TAG + "ABORT Request Received");
isDisconnected = true;
break;
default:break;
}
}
catch (final IOException e)
{
...(snip)...
}
截止OBEXConstant:
public static final byte FINAL_BIT = (byte) 0x80;
public static final byte CONNECT = 0x00 | FINAL_BIT;
//*high bit always set Connect choose your partner, negotiate capabilities
public static final byte DISCONNECT = 0x01 | FINAL_BIT;
//*high bit always set Disconnect signal the end of the session
public static final byte PUT = 0x02;
//(0x82) Put send an object
public static final byte PUT_FINAL = PUT | FINAL_BIT;
public static final byte GET = 0x03;
//(0x83) Get get an object
public static final byte GET_FINAL = GET | FINAL_BIT;
//(0x83) Get get an object
public static final byte SETPATH = 0x05;
public static final byte SETPATH_FINAL = SETPATH | FINAL_BIT;
public static final byte SESSION = 0x07;
public static final byte ABORT = (byte) 0xFF;
public static final byte OBEX_RESPONSE_CONTINUE = (byte) 0x90;
public static final byte OBEX_RESPONSE_SUCCESS = (byte) 0xA0;
另一答案【在Android 2.3上实现OBEX PUSH服务器】也许这有帮助:com.android.bluetooth.pbap.BluetoothPbapObexServer
推荐阅读
- 在Android上禁用蓝牙可发现模式
- Android和双模蓝牙设备(与BR / EDR(经典)配对时未找到自定义BLE服务)
- Android的多重连接
- Android - 检测我的应用的其他用户是否在附近
- 从android下载文件时出现套接字异常
- Android 3g下载和多线程
- Android中的文件下载[关闭]
- 哪里可以找到WordPress的Genesis儿童主题()
- 如何在WordPress中创建漂亮的登陆页面()