|
|
Overview: MobileDragon provides simple support for Bluetooth networking. It allows to perform simple game service advertisement, discovery and connection establishment. It relies on RFCOMM protocol and allows to send and receive data packets 126 bytes size. There are two main classes in MobileDragon that provide the Bluetooth support. BtNetwork: BtNetwork class manages service discovery and advertisement,
and connection establishment. You don’t have to create the instances of
this class, use “btnet” member of System object instead. //
Bluetooth service uuid for this application. //
Initialize bluetooth module. Then switch the device to specified working mode. Each active Bluetooth device can be in one of two modes: “slave” or “master”. Slave devices capable to listen for inbound connection from master device, accept single inbound connection and exchange data with master device. Slave devices can advertise themselves running the service that master device is interested about. Master device controls the entire communication, it can perform device discovery and query each found slave device for which services are running there. Master device can initiate connections to up to 7 slave devices simultaneously. Switching device to slave mode: //
Switch to slave mode. Switching device to slave mode automatically start game service advertisement and listening for inbound connection. The uuid is used for service advertisement. Switching device to master mode: //
Switch to master mode Master device then can initiate discovery: The device discovery and service inquiry sequence starts, searching for slave running game service with specified uuid. This is asynchronous process and can take up to one minute. You can poll btnet for results of discovery: //
Check if discovery is finished. GetDiscoveredSlaves() returns True when discovery process is finished and fills the vector found_hosts with BtHost objects identifying slave devices running this game service. BtHost structure contains device name, device Bluetooth address and RFCOMM service channel number. It’s time to establish connection to discovered slave device.
IBtConnection: IBtConnection class is an interface for Bluetooth endpoint connection. It provides methods for data exchange and determining connection status. Master initiate connection as follows: //
try to connect This returns new connection IBtConnection interface pointer or NULL if a connection cannot be established. Slave only accepts single connection. //
Check if there is an inbound connection accepted. This returns new connection IBtConnection interface pointer or NULL if there is no inbound connection accepted. There are two methods to determine connection state:
//
Determine that previously connected connection disconnected. To send data packets to remote host use: //
Check if connection output buffer is ready to write //
Send data to remote host. Calling connection EndWrite()
sends 126 bytes size packet to remote host. //
Drops data in input buffer and became ready to accept new one. To finish with connection object use BtNetwork CloseConnection()
method, passing Also switching between master and slaves not possible. You must first perform shutdown and then repeat initialization sequence.
Copyright 2005-2006 Herocraft Hitech Co. Ltd. |