⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bluetoothapi.cpp

📁 qaSDQDaca<FCSASDAVCASC SDVFDSVDF
💻 CPP
字号:
 /*
============================================================================================
 Name		: BluetoothAPI.cpp
 Author	    : BluetoothAPI is a initiative of Embedded LAB - http://www.embedded.ufcg.edu.br/
 			  OpenC/SymbianC++ - http://efforts.embedded.ufcg.edu.br/symbiancpp	
 Version	 :
 Copyright   : This file is part of BluetoothAPI.
               BluetoothAPI is free software: you can redistribute it and/or modify
               it under the terms of the GNU Lesser General Public License as published by
               the Free Software Foundation, either version 3 of the License, or
               (at your option) any later version.

               BluetoothAPI is distributed in the hope that it will be useful,
               but WITHOUT ANY WARRANTY; without even the implied warranty of
               MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
               GNU Lesser General Public License for more details.

               You should have received a copy of the GNU Lesser General Public License
               along with BluetoothAPI. If not, see <http://www.gnu.org/licenses/>.
 
 Description : BluetoothAPI.cpp implementation
===========================================================================================
*/

//  Include Files  

#include "BluetoothAPI.h"
#include "StringUtils.h"
using namespace std;

const TUint32 KBTServiceUUID=0x20ff;

//  Member Functions

EXPORT_C BluetoothAPI* BluetoothAPI::create(BluetoothAPIListener *aListener)
{
	BluetoothAPI* self = new BluetoothAPI(aListener);
	return self;
}

BluetoothAPI::BluetoothAPI(BluetoothAPIListener *aListener)
{
	User::LeaveIfError(iSocketServ.Connect());
	iBTDeviceDiscovery = CBluetoothDeviceSearcher::NewL(this);
	iBTServiceDiscovery = new(ELeave) CBluetoothServiceSearcher(this);
	iBTServer = CBluetoothServer::NewL(this);
	iBTServiceAdvertiser = CBluetoothServiceAdvertiser::NewL();
	APIListener = aListener;
	iIsServer = ETrue;
	iConnected = EFalse;
	iSearch = EFalse;
	isAdvertising = EFalse;
}

EXPORT_C void BluetoothAPI::searchDevices()
{
	iBTDeviceDiscovery->StartDeviceSearcherL(iSocketServ);
}

EXPORT_C void BluetoothAPI::searchServices(unsigned long int BTServiceUUID)
{
	iServiceSearch = TUUID(BTServiceUUID);
	iSearch = ETrue;
	iBTDeviceDiscovery->StartDeviceSearcherL(iSocketServ);
}

EXPORT_C void BluetoothAPI::searchServices(unsigned long int BTServiceUUID1,
		unsigned long int BTServiceUUID2, unsigned long int BTServiceUUID3,
		unsigned long int BTServiceUUID4)
{
	iServiceSearch = TUUID(BTServiceUUID1, BTServiceUUID2, BTServiceUUID3, BTServiceUUID4);
	iSearch = ETrue;
	iBTDeviceDiscovery->StartDeviceSearcherL(iSocketServ);
}

void BluetoothAPI::SearchServices()
	{
		iIndex = 0;
		ServiceList.clear();
		RBTDeviceDataArray devices = iBTDeviceDiscovery->DeviceDataList();
		TBTDeviceData* device = (devices)[iIndex];
		iBTServiceDiscovery->FindServiceByUUIDL(device->iDeviceAddr, iServiceSearch );
	}

EXPORT_C BluetoothAPI::~BluetoothAPI()
{
	iSocketServ.Close();
	if ( iBTDeviceDiscovery )
		{
		delete iBTDeviceDiscovery;
		iBTDeviceDiscovery = NULL;
		}
	stopServerAndAdvertising();
}

void BluetoothAPI::HandleBTDeviceSearchCompleteL( RBTDeviceDataArray iDeviceDataList )
{
	int numDev = iDeviceDataList.Count();
	DeviceList.clear();
	for (int i = 0; i < numDev; i++){
		BTDeviceData Aux;
		TBuf<32> iAux;
		tbufC16tochar(iDeviceDataList[i]->iDeviceName, Aux.devName);
		Aux.RfCommServicePort = iDeviceDataList[i]->iRfCommServicePort;
		iDeviceDataList[i]->iDeviceAddr.GetReadable(iAux);
		tbufC16tochar(iAux, Aux.devAdr);
		DeviceList.push_back(Aux);
	}
	APIListener->devicesDiscoverFinished( DeviceList );
	if (DeviceList.capacity() > 0 && iSearch){
		iSearch = EFalse;
		SearchServices();
	}
}

EXPORT_C vector<BTDeviceData> &BluetoothAPI::foundDevicesList(){
	return DeviceList;
}

EXPORT_C vector<BTDeviceData> &BluetoothAPI::foundServicesList(){
	return ServiceList;
}

void BluetoothAPI::OnServiceSearchComplete(TInt aPort, TInt aError){

	if (aError == KErrNone)
		{
		BTDeviceData Aux;
		TBuf<48> iAux;
		iIndice = iIndex;
		tbufC16tochar(iBTDeviceDiscovery->DeviceDataList()[iIndex]->iDeviceName, Aux.devName);
		iBTDeviceDiscovery->DeviceDataList()[iIndex]->iDeviceAddr.GetReadable(iAux);
		iBTDeviceDiscovery->DeviceDataList()[iIndex]->iRfCommServicePort = aPort;
		tbufC16tochar(iAux, Aux.devAdr);
		Aux.RfCommServicePort = aPort;
		ServiceList.push_back(Aux);
		}

	iIndex++;
	if (iIndex < iBTDeviceDiscovery->DeviceDataList().Count())
		{
		RBTDeviceDataArray devices = iBTDeviceDiscovery->DeviceDataList();
		TBTDeviceData* device = devices[iIndex];
		iBTServiceDiscovery->FindServiceByUUIDL(device->iDeviceAddr,iServiceSearch);
		}
	else
		{
		APIListener->servicesDiscoverFinished( ServiceList );
		if(ServiceList.capacity() == 1)
			{
			RBTDeviceDataArray devices = iBTDeviceDiscovery->DeviceDataList();
			TBTDeviceData* device = devices[iIndice];
			RSocket iBtSocket;
			iBTClient = CBluetoothClient::NewL(iBtSocket, this);
			iBTClient->ConnectL(iSocketServ, device->iDeviceAddr, device->iRfCommServicePort);
			}
		}
}

EXPORT_C void BluetoothAPI::connectToServer(BTDeviceData DeviceData){
	TBTDeviceData Device;
	TBuf<48> iAux;
	
	chartotbuf16(DeviceData.devName, Device.iDeviceName);
	chartotbuf16(DeviceData.devAdr, iAux);
	Device.iDeviceAddr.SetReadable(iAux);
	Device.iRfCommServicePort = DeviceData.RfCommServicePort;

	RSocket iBtSocket;
	iBTClient = CBluetoothClient::NewL(iBtSocket, this);
	iBTClient->ConnectL(iSocketServ, Device.iDeviceAddr, Device.iRfCommServicePort);
}

EXPORT_C void BluetoothAPI::startAdvertising(unsigned long int BTServiceUUID1,
		unsigned long int BTServiceUUID2, unsigned long int BTServiceUUID3,
		unsigned long int BTServiceUUID4, char* BTServiceName )
{
	isAdvertising = ETrue;
	TInt iPort = iBTServer->StartL(iSocketServ);
	TBuf16 <16> ServiceName;
	_LIT(KServiceName, "%s");
	ServiceName.Format(KServiceName, BTServiceName);
	chartotbuf16( BTServiceName, ServiceName );
	iBTServiceAdvertiser->StartAdvertisingL(iPort, BTServiceUUID1, BTServiceUUID2,
			BTServiceUUID3,	BTServiceUUID4, ServiceName);
}

EXPORT_C void BluetoothAPI::startAdvertising(unsigned long int BTServiceUUID,
		char* BTServiceName )
{
	isAdvertising = ETrue;
	TInt iPort = iBTServer->StartL(iSocketServ);
	TBuf16 <16> ServiceName;
	_LIT(KServiceName, "%s");
	ServiceName.Format(KServiceName, &BTServiceName);
	iBTServiceAdvertiser->StartAdvertisingL(iPort, BTServiceUUID, ServiceName);
}

EXPORT_C void BluetoothAPI::stopServerAndAdvertising()
{
	if (iConnected && iIsServer){
		iBTServer->Stop();
		iBTServiceAdvertiser->StopAdvertisingL();
		iConnected = EFalse;
	}
	if (isAdvertising){
		iBTServiceAdvertiser->StopAdvertisingL();
		isAdvertising = EFalse;
	}
}

EXPORT_C void BluetoothAPI::disconnectAsClient()
	{
		if (iConnected && !iIsServer){
			iBTClient->Disconnect();
			iConnected = EFalse;
		}
	}

EXPORT_C void BluetoothAPI::sendMessage(char* message)
{
	TBuf8 <KMaximumMessageLength> Message;
	chartotbuf8( message, Message);
	if (iConnected){
		if (iIsServer)
			iBTServer->SendL(Message);
		else
			iBTClient->SendL(Message);
	}
}

void BluetoothAPI::HandleBTServerConnected()
{
	iConnected = ETrue;
	isAdvertising = EFalse;
	iIsServer = ETrue;
	APIListener->serverConnected();
}

void BluetoothAPI::HandleBTServerDisconnected()
{
	iConnected = EFalse;
	APIListener->serverDisconnected();
}

void BluetoothAPI::HandleBTServerMessageRecvd()
{
	char iMessage[256];
	tbufC8tochar(iBTServer->iBuffer, iMessage);
	APIListener->messageReceived( iMessage );
}

void BluetoothAPI::HandleBTClientConnected()
{
	iIsServer = EFalse;
	iConnected = ETrue;
	APIListener->clientConnected();
}

void BluetoothAPI::HandleBTClientDisconnected()
{
	iConnected = EFalse;
	APIListener->clientDisconnected();
}

void BluetoothAPI::HandleBTClientMessageRcvd()
{
	char iMessage[256];
	tbufC8tochar(iBTClient->iBuffer, iMessage);
	APIListener->messageReceived( iMessage );
}

void BluetoothAPI::HandleBTMessageSent()
{
	APIListener->messageSent();
}

void BluetoothAPI::HandleBTError(TInt aError)
{
	APIListener->errorOcurred(aError);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -