Bluetooth Application Development—use winsock

本文介绍了在Windows CE上使用Winsock进行蓝牙应用开发,包括服务端和客户端的基本操作流程。服务端涉及socket创建、绑定、监听和接受连接,客户端则涉及查找设备、连接服务。开发过程中主要依赖于Winsock接口,利用RFCOMM协议实现蓝牙通信任务。
摘要由CSDN通过智能技术生成

一. 开发环境

硬件: HTC3238+WM6.1

SDK: STANDARDSDK_500

二. 目标

学习winsock方式的蓝牙应用开发

三. 背景介绍

In Microsoft® Windows® CE, the primary way an application can use Bluetooth is through the Winsock interface, which exposes RFCOMM protocol. Virtual COM ports are also exposed, but this interface is designed to be enabled under existing OS services such as UNIMODEM, GPS, or terminal emulators. By using various protocols and profiles, Bluetooth can be implemented to perform the following tasks:

  • Connect to a modem through a cellular phone.
  • Connect to a local area network (LAN) access point.
  • Enable object exchange and synchronization using the Object Exchange Protocol (OBEX).
  • Use wireless headset and hands-free devices to handle calls on a cellular phone.
  • Transfer data from a desktop computer to a mobile device over a wireless connection.

For more information about Bluetooth technology, see this Official Bluetooth Wireless Info Web site.

摘自:http://msdn.microsoft.com/en-us/library/ms847082.aspx

从上面的文字中可以看出在WM平台上,有两种方式可以操作蓝牙:Winsock和Virtual COM ports.这里我采用了Winsock方式.

四. 基本操作流程

1. 服务端

按MSDN上的说法,蓝牙的WINSOCK操作方式和基本的socket-based差不多.

SOCKET s = socket(...);
bind(s, ...);//绑定到一个RFCOMM通道上,exports the SDP record advertising this channel

listen(s, ...);
for(; ;)
{
   SOCKET s2= accept(s, ...);
   SpinThreadsForConnection(s2);
}

示例代码:

//Function: OpenServerConnection
//Purpose:    Opens a server socket for listening. Registers the service. Creates a thread, ReadThread for reading incoming messages.
//Input:    The SDP record of the service to register, size of the SDP record, channel offset in the record, pointer to the UI function that displays the messages in the UI
//Return: If error occurs, returns the appropriate WSAGetLastError, otherwise returns zero.
//注:调用语句是iRetVal=objBthUtils.OpenServerConnection(rgbSdpRecord, SDP_RECORD_SIZE, SDP_CHANNEL_OFFSET, DisplayMessage);
//rgbSdpRecord,SDP_RECORD_SIZE, SDP_CHANNEL_OFFSET是事先由bthnscreate.exe生成的,bthnscreate.exe的源码在D:/WINCE500/PUBLIC/COMMON/SDK/SAMPLES/BLUETOOTH/BTHNSCREATE,这是PB5的安装目录

int BthUtils::OpenServerConnection(BYTE *rgbSdpRecord, int cSdpRecord, int iChannelOffset, void (*funcPtr)(WCHAR *))
{
    int iNameLen=0;
    if(m_socketServer==INVALID_SOCKET)
    {
        m_socketServer = socket (AF_BT, SOCK_STREAM, BTHPROTO_RFCOMM);
        if (m_socketServer  == INVALID_SOCKET)
        {
            return WSAGetLastError ();
        }

        SOCKADDR_BTH sa;
        memset (&sa, 0, sizeof(sa));
        sa.addressFamily = AF_BT;
        sa.port = 0;
        if (bind (m_socketServer, (SOCKADDR *)&sa, sizeof(sa)))
        {
            return WSAGetLastError ();
        }
        iNameLen = sizeof(sa);
        if (getsockname(m_socketServer, (SOCKADDR *)&sa, &iNameLen))   
        {
            return WSAGetLastError ();
        }

        if(RegisterService(rgbSdpRecord, cSdpRecord, iChannelOffset, (UCHAR)sa.port)!=0)
            return WSAGetLastError();

        if (listen (m_socketServer, SOMAXCONN))
        {
            return WSAGetLastError ();
        }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值