Managed wrapper for the BTObex library
Avishkar Autar · May 12 2010 · Bluetooth Technology
More open-source goodness. A managed wrapper for the BTObex library. The source includes the BTObexWrapper.h header file which contains the wrapper code (shown below) and a compiled managed DLL, for those who just want to include it into their projects and use it ASAP.
// Managed C++/CLI wrapper for the BTObex Library
// Include the BTObex library files into the project (BTOBEX sub-directory) and compile as a managed DLL
/*
Copyright (c) 2010, Avishkar Autar
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the <organization> nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#pragma unmanaged
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <winsock2.h>
#include <Ws2bth.h>
#include <BluetoothAPIs.h>
#pragma comment(lib, "ws2_32.lib")
#include <windows.h>
#include <cguid.h>
#include "BTOBEX/BTSock.h"
#include "BTOBEX/OBEX.h"
#include "BTOBEX/BTOBEX.h"
#pragma managed
using namespace System;
using namespace System::Runtime::InteropServices;
namespace BTObex
{
public ref class BluetoothDeviceProfile
{
public:
System::String^ name;
System::String^ address; // (address):port, typically repeat of device address
int port;
};
public ref class BluetoothDevice
{
public:
System::String^ name;
System::String^ localAddress;
System::String^ deviceAddress;
System::Collections::ArrayList^ supportedProfiles;
virtual System::String^ ToString() override
{
return name;
}
};
public ref class Progress
{
public:
System::IntPtr^ current;
System::IntPtr^ total;
};
public ref class BTObexFileIO
{
protected:
BTOBEX::Session* btSession;
BTOBEX::Progress* opProgress;
wchar_t* MarshalStringToCStr(System::String^ str)
{
wchar_t* ret = new wchar_t[str->Length+1];
for(int i=0; i<str->Length; i++)
ret[i] = str[i];
ret[str->Length] = 0;
return ret;
}
void DeleteMarshaledCStr(wchar_t* str)
{
delete [] str;
str = 0;
}
public:
BTObexFileIO() : btSession(0)
{
opProgress = new BTOBEX::Progress();
}
~BTObexFileIO()
{
delete opProgress;
}
void QueryDevices(System::Collections::ArrayList^ devices, bool queryOnlyOBEXFileTransferProfiles)
{
BTSock::Session* btSession = new BTSock::Session();
std::vector<BTSock::Device*> btDevices;
if(queryOnlyOBEXFileTransferProfiles)
{
btSession->DeviceQueryObject()->PerformQuery(btDevices, OBEXFileTransferServiceClass_UUID);
}
else
{
btSession->DeviceQueryObject()->PerformQuery(btDevices);
}
for(int i=0; i<(int)btDevices.size(); i++)
{
BluetoothDevice^ dev = gcnew BluetoothDevice();
dev->name = gcnew System::String( btDevices[i]->name.c_str() );
dev->localAddress = gcnew System::String( btDevices[i]->localAddress.c_str() );
dev->deviceAddress = gcnew System::String( btDevices[i]->deviceAddress.c_str() );
dev->supportedProfiles = gcnew System::Collections::ArrayList();
for(int j=0; j<(int)btDevices[i]->supportedProfiles.size(); j++)
{
BluetoothDeviceProfile^ profile = gcnew BluetoothDeviceProfile();
profile->name = gcnew System::String( btDevices[i]->supportedProfiles[j]->name.c_str() );
profile->address = gcnew System::String( btDevices[i]->supportedProfiles[j]->address.c_str() );
profile->port = btDevices[i]->supportedProfiles[j]->port;
dev->supportedProfiles->Add(profile);
}
devices->Add(dev);
}
delete btSession;
}
bool Connect(System::String^ address, int port, int packetSize)
{
wchar_t* addr = MarshalStringToCStr(address);
btSession = new BTOBEX::Session();
bool connOk = btSession->Connect(addr, port, packetSize);
DeleteMarshaledCStr(addr);
return connOk;
}
void Disconnect()
{
btSession->Disconnect();
delete btSession;
}
bool SetPathBackward()
{
return btSession->SetPathBackward();
}
bool SetPathForward(System::String^ folderName)
{
wchar_t* folderNameCStr = MarshalStringToCStr(folderName);
bool setOk = btSession->SetPathForward(folderNameCStr);
DeleteMarshaledCStr(folderNameCStr);
return setOk;
}
bool GetFolderListing(System::Collections::ArrayList^ result)
{
OBEX::NakedVector<OBEX::byte> utf8Bytes;
bool getOk = btSession->GetFolderListing(utf8Bytes);
if(getOk)
{
for(int i=0; i<utf8Bytes.Count(); i++)
{
result->Add( gcnew System::Byte(utf8Bytes[i]) );
}
}
return getOk;
}
bool DeleteObject(System::String^ objName)
{
wchar_t* objNameCStr = MarshalStringToCStr(objName);
bool delOk = btSession->DeleteObject(objNameCStr);
DeleteMarshaledCStr(objNameCStr);
return delOk;
}
bool PutFile(System::String^ remoteFileName, System::String^ localFilePath, Progress^ progress)
{
progress->current = gcnew System::IntPtr( &opProgress->current );
progress->total = gcnew System::IntPtr( &opProgress->total );
wchar_t* remoteFileNameCStr = MarshalStringToCStr(remoteFileName);
wchar_t* localFilePathCStr = MarshalStringToCStr(localFilePath);
bool putOk = btSession->PutFile(remoteFileNameCStr, localFilePathCStr, opProgress);
DeleteMarshaledCStr(remoteFileNameCStr);
DeleteMarshaledCStr(localFilePathCStr);
return putOk;
}
bool GetFile(System::String^ remoteFileName, System::String^ localFilePath, Progress^ progress)
{
progress->current = gcnew System::IntPtr( &opProgress->current );
progress->total = gcnew System::IntPtr( &opProgress->total );
wchar_t* remoteFileNameCStr = MarshalStringToCStr(remoteFileName);
wchar_t* localFilePathCStr = MarshalStringToCStr(localFilePath);
bool getOk = btSession->GetFile(remoteFileNameCStr, localFilePathCStr, opProgress);
DeleteMarshaledCStr(remoteFileNameCStr);
DeleteMarshaledCStr(localFilePathCStr);
return getOk;
}
};
}