小弟开发android,遇到关于基于蓝牙的"电话本访问协议(PBAP)"的一个问题。。

D_ning 2013-02-03 06:08:46
小弟在Android系统上开发一个通过蓝牙获取他机联系人及通话记录的程序,参考相关资料后采用了PBAP协议,首先通过UUID:0000112f-0000-1000-8000-00805f9b34fb 建立BluetoothSocket连接。建立socket连接后,根据IrOBEX文档所述规则及PBAP Specification的描述,通过socket的IO流向他机传字节流,首先是写入request:
0x80 //Opcode

0x00 //packet length
0x1f

0x10 //Version 1.0 of OBEX
0x00 //flag
0x20 //8K maximum packet size
0x00

0x46 //header of target
0x00 //length
0x13

0x79 //pbap target 796135f0-f0c5-11d8-0966-0800200c9a66
0x61
0x35
0xf0
0xf0
0xc5
0x11
0xd8
0x09
0x66
0x08
0x00
0x20
0x0c
0x9a
0x66

0xc3 //irOBEX里说是 一个length header 不知道有啥用
0x00 //length为十六进制 f483
0x00
0xf4
0x83


将这一长段字节码传走后,从他机得到reaponse packet
0xa0 //成功

0x00
0x1f
0x10
0x00

0xff //他机可提供的最大packet size
0xfe

0xcb //Connection ID
0x00
0x00
0x00
0x01

0x4a //Header of Who
0x00
0x13

0x79 //pbap target 796135f0-f0c5-11d8-0966-0800200c9a66
0x61
0x35
0xf0
0xf0
0xc5
0x11
0xd8
0x09
0x66
0x08
0x00
0x20
0x0c
0x9a
0x66

得到这样的反馈即表明对OBEX服务的连接已经成功,pbap服务启动,之后进行get操作,写request字节流:


os.write((0x83));//Get Header 0x83
os.write(0x00);
os.write(0x4F);
///

os.write(0xCB);//Connection ID 0xCB
os.write(0x00);
os.write(0x00);
os.write(0x00);
os.write(0x01);




os.write(0x01);//Name
os.write(0x00);
os.write(0x21);


char[] cs = new char[] { 't', 'e', 'l', 'e', 'c', 'o', 'm','/' ,'p' ,'b' ,'.' ,'v' ,'c' ,'f'};//要转换的char数组
String str = new String(cs);
byte[] bs = str.getBytes("Unicode");
/*
for(int i0 = 0;i0<bs.length;i0++)
{
bs[i0] = (byte) (bs[i0]&0xff);
}
*/
os.write(bs);





os.write(0x42);//Type
os.write(0x00);
os.write(0x12);


char[] cs2 = new char[] { 'x', '-', 'b', 't', '/', 'p', 'h','o' ,'n' ,'e' ,'b' ,'o' ,'o' ,'k'};//要转换的char数组
String str2 = new String(cs2);
byte[] bs2 = str2.getBytes();
/*
for(int i1 = 0;i1<bs2.length;i1++)
{
bs2[i1] = (byte) (bs2[i1]&0xff);
}
*/
os.write(bs2);
os.write(0x00);

os.write(0x4C);//AppParam
os.write(0x00);//appParam length
os.write(0x14);

os.write(0x06);//Tag for Filter
os.write(0x08);//length field


os.write(0x00);//不对vcard做任何过滤 os.write(0x00);
os.write(0x00);
os.write(0x00);
os.write(0x00);
os.write(0x00);
os.write(0x00);
os.write(0x00);





os.write(0x07);//Tag for Format
os.write(0x01);//length field
os.write(0x01);//format value vcard 3.0

os.write(0x04);//Tag for MLC
os.write(0x02);//length
os.write(0xff);//0xff
os.write(0xff);//0xff



之后得到一个8字节长的response packet,opcode为C6,查询irOBEX,C6对应一个错误码,是 Not Acceptable
同时在PBAP specification上找到对Not Acceptable的一个解释:
The request is recognized and all the parameter values are legal, but there is a problem with a parameter value that indicates a request that cannot be met by the Server.
翻译一下既是:请求被识别且参数合法,但是参数不能被服务器(即他机)处理。



但是这样的一个request是参照PBAP的官方specificati所写,所以小弟十分困惑,期待各路达人的解救,百分送上!万分感谢!~~~~~~^-^
...全文
997 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
a3372156 2015-08-15
  • 打赏
  • 举报
回复
引用 3 楼 comeoverjava 的回复:
楼主给个联系方式吧,真心有问题请教!跪求!!!
你搞定没 求救啊
a3372156 2015-08-15
  • 打赏
  • 举报
回复
楼主完成了那个吗 能不能分享一下 641246913@qq.com
ys2156li 2015-03-31
  • 打赏
  • 举报
回复
这个链接的ID connect Id 应该是多少
ys2156li 2015-03-31
  • 打赏
  • 举报
回复
楼主 phoneBookGetSocket这个是怎么写的 可不可以给我说说
haochedai 2015-03-23
  • 打赏
  • 举报
回复
好流掰的样子
fandongge_ 2015-03-23
  • 打赏
  • 举报
回复
能否发我一份完整的代码到我邮箱 715148279@qq.com非常感谢~
qymfly 2015-03-16
  • 打赏
  • 举报
回复
能否发我一份完整的代码到我邮箱 qiuyuemingfly@163.com 非常感谢
lichunli8866 2014-07-17
  • 打赏
  • 举报
回复
能否发我一份完整的代码到我邮箱 soku_123@163.com 非常感谢
u014224387 2014-03-19
  • 打赏
  • 举报
回复
上述代码是从我的工程里拷贝过来的,经测试可以获得电话薄,希望对你有帮助~~
u014224387 2014-03-19
  • 打赏
  • 举报
回复
OutputStream os; InputStream is; try { os = phoneBookGetSocket.getOutputStream(); is = phoneBookGetSocket.getInputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); baos.write((byte)0x83);// get(也可以为03) baos.write((byte)0x00);// 整个数据包的长度 高字节 baos.write((byte)0x4f);// 整个数据包的长度 低字节 baos.write((byte)0xcb);// connect id baos.write(connectId);//connect id 值 baos.write((byte)0x01);//name byte[] nameBytes = new byte[]{ 0x00,0x74,0x00,0x65,0x00,0x6c,0x00,0x65,0x00,0x63,0x00,0x6f,0x00,0x6d,0x00, 0x2f,0x00,0x70,0x00,0x62,0x00,0x2e,0x00,0x76,0x00,0x63,0x00,0x66 }; short nameLength = (short) (nameBytes.length + 2 + 3); byte[] nameLengthBytes = shortToBytes(nameLength); baos.write(nameLengthBytes);//name 长度2字节 baos.write(nameBytes);//name的长度 高字节 baos.write((byte)0x00);//name结束 baos.write((byte)0x00);//name结束 baos.write((byte)0x42);//type byte[] typeBytes = "x-bt/phonebook".getBytes("UTF-8"); short typeLength = (short) (typeBytes.length + 1 + 3); byte[] typeLengthBytes = shortToBytes(typeLength); baos.write(typeLengthBytes);//type长度2字节 baos.write(typeBytes);//type name baos.write((byte)0x00);//type name 结束 baos.write((byte)0x4c);//app params baos.write((byte)0x00);//app params 的长度 高字节 baos.write((byte)0x14);//app params 的长度 低字节 baos.write((byte)0x06);//此处为pbap自定义,表示vcardfilter baos.write((byte)0x08);// 8位 baos.write(new byte[8]);// 64位掩码.需要的话请看spec.全为0,则返回所有的 baos.write((byte)0x07);// vcard 版本 baos.write((byte)0x01);// 长度 baos.write((byte)0x01);// 01= 3.1 00 = 2.0 baos.write((byte)0x04);//maxlistcount 取多少个 baos.write((byte)0x02);// 长度 baos.write((byte)0xff);// ffff表示取所有的. baos.write((byte)0xff);// ffff表示取所有的. byte[] phoneBookDownloadReq = baos.toByteArray(); System.out.println("send data =" + bytesToHexString(phoneBookDownloadReq.length, phoneBookDownloadReq)); os.write(phoneBookDownloadReq); os.flush(); byte[] data = new byte[1024]; int len = 0; while ((len = is.read(data)) > 0) { System.out.println(bytesToHexString(len, data)); if (len > 0) { if (data[0] == (byte) 0xa0) { break; } } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
深夜深几许 2013-04-02
  • 打赏
  • 举报
回复
楼主给个联系方式吧,真心有问题请教!跪求!!!
luckxixu 2013-03-30
  • 打赏
  • 举报
回复
我最近也在研究这个,不知道楼主研究的怎么样了,求资料!
Linfanfeng 2013-03-29
  • 打赏
  • 举报
回复
有没有试一下SetPhoneBook 请求能不能走通?

80,354

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧