Salut Olivier101
Merci d'avoir vit reagit à mon message
Voici mon Code complet j'avoue que je l'ai pris d'un Example sur le Net:
CODE
/******************************************************************************
*
* Copyright © 1999-2004 PalmSource, Inc. All rights reserved.
*
* File: AppMain.c
*
*****************************************************************************/
#include <PalmOS.h>
#include "BtCommVdrv.h"
#include "BT_SRC.h"
/***********************************************************************
*
* Entry Points
*
***********************************************************************/
/***********************************************************************
*
* Internal Constants
*
***********************************************************************/
#define appFileCreator 'STRT' // register your own at [url=http://www.palmos.com/dev/creatorid/]http://www.palmos.com/dev/creatorid/[/url]
#define appVersionNum 0x01
#define appPrefID 0x00
#define appPrefVersionNum 0x01
#define AppCreator '???2' //TODO set your Application Creator ID
#define sysFileTApplication 'appl'
#define MinVersion 0x05000000 //TODO set your minimal version number here (this is 5.0)
typedef struct {
//TODO declare here your preference variables
UInt8 cAddress[1];
} Prefs;
static Prefs prefs;
/* BT Globals */
/**************/
static UInt16 unPortId;
static UInt16 btLibRefNum;
static Err err;
static SrmOpenConfigType config;
static BtVdOpenParams btParams;
static BtLibSdpUuidType sppUuid;
/***********************************************************************
*
* Internal Functions
*
***********************************************************************/
/**************************************/
/* Close a Bluetooth serial connetion */
/**************************************/
static void BT_Close()
{
if(unPortId)
{
WinDrawChars("Close ",10,80,0);
SrmClose(unPortId);
unPortId=0;
}
}
/*************************************/
/* Open a Bluetooth serial connetion */
/*************************************/
static void BT_Open()
{
BT_Close();
MemSet(&sppUuid, sizeof(sppUuid), 0);
sppUuid.size = btLibUuidSize16;
sppUuid.UUID[0] = 0x11;
sppUuid.UUID[1] = 0x01;
MemSet(&btParams, sizeof(btParams), 0);
btParams.u.client.remoteDevAddr.address[0]=prefs.cAddress[0];
btParams.u.client.remoteDevAddr.address[1]=prefs.cAddress[1];
btParams.u.client.remoteDevAddr.address[2]=prefs.cAddress[2];
btParams.u.client.remoteDevAddr.address[3]=prefs.cAddress[3];
btParams.u.client.remoteDevAddr.address[4]=prefs.cAddress[4];
btParams.u.client.remoteDevAddr.address[5]=prefs.cAddress[5];
btParams.role = btVdClient;
btParams.u.client.method = btVdUseUuidList;
btParams.u.client.u.uuidList.tab = &sppUuid;
btParams.u.client.u.uuidList.len = 1;
MemSet(&config, sizeof(config), 0);
config.function = serFncUndefined;
config.drvrDataP = (MemPtr)&btParams;
config.drvrDataSize = sizeof(btParams);
err=SrmExtOpen(sysFileCVirtRfComm,//Type of Port==Bluetooth RFComm
&config,//Port configuration Params
sizeof(config),//Laege von port Params
&unPortId//Emfang von ID von diesem port instance
);
if(err==0)
WinDrawChars("Open ",10,80,0);
else
WinDrawChars("Error1 ",10,80,0);
}
static void BT_FindDevice()
{
BT_Close();
if(btLibRefNum)
{
err=BtLibOpen(btLibRefNum,false);
if(err==0)
{
err=BtLibDiscoverSingleDevice(btLibRefNum,NULL,NULL,0,
(BtLibDeviceAddressType *)(prefs.cAddress),false,true);
if(err)
WinDrawChars("Error2 ",10,80,0);
else
WinDrawChars("Found ",10,80,0);
}
BtLibClose(btLibRefNum);
}
}
/***********************/
/* Load the BT library */
/***********************/
static void BT_LoadLibrary()
{
btLibRefNum=0;
err=SysLibFind("Bluetooth Library",&btLibRefNum);
if(err)
err=SysLibLoad(sysFileTLibrary,sysFileCBtLib,&btLibRefNum);
if(err)
WinDrawChars("Error3 ",10,80,0);
}
/****************/
/* Flush BT ser */
/****************/
static void BT_Flush(UInt16 unTimeout)
{
if(unPortId)
{
err=SrmReceiveFlush(unPortId,unTimeout);
if(err)
WinDrawChars("Error4 ",10,80,0);
}
}
/****************/
/* Send BT data */
/****************/
static void BT_Send()
{
UInt32 tosend,Nachricht;
char msg[10];
5r7t
msg[0]=0x43;
msg[1]=0x06;
msg[2]=0x00;
msg[3]=0x01;
msg[4]=0x00;
msg[5]=0x52;
msg[6]=0xFF;
msg[7]=0xFF;
msg[8]=0x17;
msg[9]=0x0D;
tosend =sizeof(msg);
if(unPortId)
{
Nachricht=SrmSend(unPortId,msg,tosend,&err);
if(err)
WinDrawChars("Error5 ",10,80,0);
else
WinDrawChars("Okay5 ",10,80,0);
}
}
/*************************************************/
/* You may need to preprocess events, do it here */
/*************************************************/
Boolean PreprocessEvent(EventPtr eventP)
{
Boolean handled = false;
WChar chrPress;
chrPress=eventP->data.keyDown.chr;
switch(eventP->eType)
{
case keyDownEvent:
switch(chrPress) //TODO you may wish to handle key events...
{
case vchrHard1:// button 1
BT_FindDevice();
BT_Open();
BT_Flush(5000);
handled=true;
break;
case vchrHard2:// button 2
BT_Send();
handled=true;
break;
case vchrHard4:// button 4
BT_Close();
handled=true;
break;
default:
break;
}
break;
default:
break;
}
return handled;
}
/***********************************************************************
*
* FUNCTION: MainFormHandleEvent
*
* DESCRIPTION: This routine is the event handler for the
* "MainForm" of this application.
*
* PARAMETERS: pEvent - a pointer to an EventType structure
*
* RETURNED: true if the event has handle and should not be passed
* to a higher level handler.
*
* REVISION HISTORY:
*
*
***********************************************************************/
static Boolean MainFormHandleEvent(EventPtr eventP)
{
Boolean handled = false;
FormPtr frmP = FrmGetActiveForm();
UInt16 unFrmID;
switch(eventP->eType)
{
case frmOpenEvent:// open a form
FrmDrawForm(frmP);
unFrmID=FrmGetActiveFormID();
if(unFrmID==MainForm)// it's the main form
{
}
handled = true;
break;
// case menuEvent:
//handled=doMenu(frmP, eventP->data.menu.itemID);
// break;
case ctlSelectEvent:
switch (eventP->data.ctlSelect.controlID)
{
case FIND_BUTT:
BT_FindDevice();
handled = true;
break;
case CONN_BUTT:
BT_Open();
BT_Flush(2000);
handled = true;
break;
case SEND_BUTT:
BT_Send();
handled = true;
break;
case CLOSE_BUTT:
BT_Close();
handled = true;
break;
default:
break;
}
break;
default:
break;
}
return handled;
}
/***********************************************************************
*
* FUNCTION: AppHandleEvent
*
* DESCRIPTION: This routine loads form resources and set the event
* handler for the form loaded.
*
* PARAMETERS: event - a pointer to an EventType structure
*
* RETURNED: true if the event has handle and should not be passed
* to a higher level handler.
*
* REVISION HISTORY:
*
*
***********************************************************************/
static Boolean AppHandleEvent(EventType* pEvent)
{
UInt16 formId;
FormType* pForm;
Boolean handled = false;
if (pEvent->eType == frmLoadEvent) {
// Load the form resource.
formId = pEvent->data.frmLoad.formID;
pForm = FrmInitForm(formId);
FrmSetActiveForm(pForm);
// Set the event handler for the form. The handler of the currently
// active form is called by FrmHandleEvent each time is receives an
// event.
switch (formId) {
case MainForm:
FrmSetEventHandler(pForm, MainFormHandleEvent);
break;
default:
break;
}
handled = true;
}
return handled;
}
/***********************************************************************
*
* FUNCTION: AppStart
*
* DESCRIPTION: Get the current application's preferences.
*
* PARAMETERS: nothing
*
* RETURNED: Err value errNone if nothing went wrong
*
* REVISION HISTORY:
*
*
***********************************************************************/
static Err AppStart(void)
{
UInt16 prefSize = sizeof(Prefs);
FrmGotoForm(MainForm);
//Load application preferences
if((PrefGetAppPreferences(AppCreator,0,&prefs,
&prefSize,true)==noPreferenceFound)
||(prefSize != sizeof(prefs)))
{
//TODO set default preference values here
prefs.cAddress[0]=0;
prefs.cAddress[1]=0;
prefs.cAddress[2]=0;
prefs.cAddress[3]=0;
prefs.cAddress[4]=0;
prefs.cAddress[5]=0;
PrefSetAppPreferences(appFileCreator,0,1,&prefs,sizeof(Prefs),true);
}
unPortId = 0;
btLibRefNum = 0;
BT_LoadLibrary();
return errNone;
}
/***********************************************************************
*
* FUNCTION: AppStop
*
* DESCRIPTION: Save the current state of the application.
*
* PARAMETERS: nothing
*
* RETURNED: nothing
*
* REVISION HISTORY:
*
*
***********************************************************************/
static void AppStop(void)
{
// Close all the open forms.
FrmCloseAllForms();
}
/***********************************************************************
*
* FUNCTION: AppEventLoop
*
* DESCRIPTION: This routine is the event loop for the application.
*
* PARAMETERS: nothing
*
* RETURNED: nothing
*
* REVISION HISTORY:
*
*
***********************************************************************/
static void AppEventLoop(void)
{
Err error;
EventType event;
do {
EvtGetEvent(&event, evtWaitForever);
if (SysHandleEvent(&event))
continue;
if (MenuHandleEvent(0, &event, &error))
continue;
if (AppHandleEvent(&event))
continue;
FrmDispatchEvent(&event);
} while (event.eType != appStopEvent);
}
/***********************************************************************
*
* FUNCTION: PilotMain
*
* DESCRIPTION: This is the main entry point for the application.
*
* PARAMETERS: cmd - word value specifying the launch code.
* cmdPB - pointer to a structure that is associated with the launch code.
* launchFlags - word value providing extra information about the launch.
* RETURNED: Result of launch
*
* REVISION HISTORY:
*
*
***********************************************************************/
UInt32 PilotMain(UInt16 cmd, MemPtr cmdPBP, UInt16 launchFlags)
{
Err error = errNone;
switch (cmd) {
case sysAppLaunchCmdNormalLaunch:
if ((error = AppStart()) == 0) {
AppEventLoop();
AppStop();
}
break;
default:
break;
}
return error;
}