Join Now
Quality Rating:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)
Expertise Level:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)

This page was last modified 05:48, 27 November 2007.

How to detect a bluetooth device being in non-discoverable (private) mode

From Forum Nokia Wiki

It is simple to detect all devices being in discoverable (public) mode. There is "btdiscovery" example in S60 v2 SDKs illustrating how to do this. A short description is also here: A_simple_way_of_discovering_and_selecting_a_single_Bluetooth_device

Unfortunately there is no good method of discovering devices being in non-discoverable mode. However, it is possible to detect a specific device if its bluetooth address is known. It can be done by opening a connection to the device. There are two challenges with this approach:

a) different devices implement different features and a device can be detected only if it listens on the PSM (bluetooth port) we are trying to connect to,

b) any connection attempt on a non-paired device results in a confirmation dialog displayed on the detected device which must be accepted by the user to let detect the device.

Both issues can be solved by taking advantage of the fact all bluetooth devices should implement SDP (Service Discovery Protocol). SDP itself is implemented on top of low level L2CAP protocol, it listens on PSM == 0x01 and connections to it do not require to be accepted by the device user.

This approach has been successfuly implemented in BlueRadar.

The code is as follows:

// declarations
RSocketServ iSocketServer;
 
// detect device with bluetooth address in devAddr
void CBtDetectorEngine::DetectL(TBTDevAddr devAddr)
{
    User::LeaveIfError(iSocketServer.Connect());
    TBTSockAddr btSockAddr;
    btSockAddr.SetBTAddr(devAddr);
    btSockAddr.SetPort(0x01); // SDP PSM
    iConn = CBtDetectorConnection::NewL(iSocketServer);
    iConn->SetBTSockAddr(btSockAddr);
    iConn->Connect();
}
 
 
// BtDetectorConnection.h
 
#include <e32base.h>
#include <es_sock.h>
#include <bt_sock.h>
 
class CBtDetectorConnection : MBluetoothSocketNotifier
{
public:
    static CBtDetectorConnection* NewL(RSocketServ& aSocketServer);
    virtual ~CBtDetectorConnection();
    void SetBTSockAddr(TBTSockAddr& aBTSockAddr);
    void Connect();
    void Cancel();
private:
    CBtDetectorConnection(RSocketServ& aSocketServer);
    void ConstructL();
private:
    void HandleConnectCompleteL(TInt aErr);
    void HandleAcceptCompleteL(TInt aErr) {};
    void HandleShutdownCompleteL(TInt aErr);
    void HandleSendCompleteL(TInt aErr) {};
    void HandleReceiveCompleteL(TInt aErr) {};
    void HandleIoctlCompleteL(TInt aErr) {};
    void HandleActivateBasebandEventNotifierCompleteL(TInt aErr, TBTBasebandEventNotification& aEventNotification) {};
private:
    RSocketServ& iSocketServer;
    TBTSockAddr iBTSockAddr;
    CBluetoothSocket* iSocket;
};
 
// BtDetectorConnection.cpp
 
#include <bttypes.h>
#include "BtDetectorConnection.h"
 
CBtDetectorConnection* CBtDetectorConnection::NewL(RSocketServ& aSocketServer)
{
    CBtDetectorConnection* self=new(ELeave) CBtDetectorConnection(aSocketServer);
    CleanupStack::PushL(self);
    self->ConstructL();
    CleanupStack::Pop(self);
    return self;
}
 
void CBtDetectorConnection::ConstructL()
{
    iSocket = CBluetoothSocket::NewL(*this, iSocketServer, KSockSeqPacket, KL2CAP);
}
 
CBtDetectorConnection::~CBtDetectorConnection()
{
    Cancel();
    CBluetoothSocket* s = iSocket;
    iSocket = NULL;
    delete s;
}
 
void CBtDetectorConnection::SetBTSockAddr(TBTSockAddr& aBTSockAddr)
{
    iBTSockAddr = aBTSockAddr;
}
 
void CBtDetectorConnection::Connect()
{
    Cancel();
    iSocket->Connect(iBTSockAddr);
}
 
void CBtDetectorConnection::HandleConnectCompleteL(TInt aErr)
{
    if (aErr == KErrNone)
    {
        iSocket->Shutdown(RSocket::ENormal);
        // here is the place for notifying that the device has been detected
    }
}
 
void CBtDetectorConnection::Cancel()
{
    iSocket->CancelConnect();
}
Related Discussions
Thread Thread Starter Forum Replies Last Post
Publish & subscribe keys for bluetoot patrickfrei General Symbian C++ 7 2007-10-31 09:37
How to detect device switch off from exe nroyer General Symbian C++ 3 2008-05-14 21:16
Creating a connection via WLAN Ad-Hoc Mode primal Wired and Wireless interfaces 1 2008-02-16 14:04
detect incoming bluetooth or infra-red request Rx-lee General Symbian C++ 0 2004-12-17 11:21
NCF autodetect not working jzxu Symbian Tools & SDKs 3 2006-12-10 22:38
 
Powered by MediaWiki
     
     RDF Facets:
     
     
     qfnZtopicQUqfnTopicZbluetoothQ
     qfnZtopicQUqfnTopicZconnectivityQ
     qfnZtopicQUqfnTopicZseriesE5f60Q
     qfnZtypeQUqfnTypeZCommunityContentQ
     qfnZtypeQUqfnTypeZWebpageQ
     qfnZtypeQUqfnTypeZWikiContentQ
     qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX