PDA

View Full Version : مشکل در برقراری ارتباط با bluetooth



nokia2012
پنج شنبه 14 مهر 1390, 13:05 عصر
سلام به تمام دوستان
پیشاپیش تولد امام رضا را به همگی تبریک و تهنیت عرض می کنم.
یه برنامه در رابطه با بلوتوث دارم که دستگاه هارو و سرویسها رو سرچ می کنه ولی میخام با سرویسی که انتخاب کردم رابطه داشته باشم با متد getConnectionURL که این کار تو متد senddata انجام میشه ولی هیچ کاری نمی کنه و حتی پیغام امنیتی هم نمیده .
لطفا اگه کسی اطلاعاتی داره منو راهنمایی کنید.

با تشکر فراوان از شما دوستان عزیز


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package Main;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.Buffer;
import java.util.Vector;
import javax.bluetooth.DataElement;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.DiscoveryListener;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.ServiceRecord;
import javax.bluetooth.UUID;
import javax.microedition.io.Connection;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.io.StreamConnection;
import javax.microedition.io.file.FileConnection;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.xml.rpc.Operation;
import javax.obex.ClientSession;
import javax.obex.HeaderSet;
import javax.obex.ResponseCodes;

/**
*
* @author family
*/
public class BTLClass extends Canvas implements DiscoveryListener{

DiscoveryAgent agent;
StringBuffer buff;
LocalDevice local;
Vector devices;
Vector service;
int current=0;
int chk=0;
StringBuffer str;
ServiceRecord[] serviceR;
String URL="";
String[] result;


public BTLClass()
{
FindDevices();
}

//__________________________________________________ _______________________________
public void FindDevices(){
try{
devices = new Vector();
local = LocalDevice.getLocalDevice();
agent = local.getDiscoveryAgent();
agent.startInquiry(DiscoveryAgent.GIAC,this);
}catch(Exception e){}
}
//__________________________________________________ _______________________________
public void sendData(String URL)
{
String data="hello world...";
String name="sample.txt";
// String url="btgoep://001939C47007:6";
try {
ClientSession conn=(ClientSession)Connector.open(URL, Connector.READ_WRITE);
HeaderSet HS=conn.connect(null);
if(HS.getResponseCode()==ResponseCodes.OBEX_HTTP_O K || HS.getResponseCode()==ResponseCodes.OBEX_HTTP_ACCE PTED)
{
HeaderSet hs=conn.createHeaderSet();
hs.setHeader(HeaderSet.LENGTH, new Long(data.length()));
hs.setHeader(HeaderSet.NAME, name );
javax.obex.Operation op=conn.put(hs);
OutputStream out=op.openOutputStream();
// while ()
// {
out.write(data.getBytes());
out.close();
op.close();
conn.disconnect(null);
conn.close();
// synchronized(this) {wait(2000);}
// }
}
} catch (Exception e) {
// e.printStackTrace();
}

}
//__________________________________________________ _______________________________
public void deviceDiscovered(javax.bluetooth.RemoteDevice rd,javax.bluetooth.DeviceClass dc)
{
devices.addElement(rd);
}
//__________________________________________________ _______________________________
public void FindServices(RemoteDevice device){
try{
int[] att={0x0100};
UUID[] uuids = new UUID[1];
uuids[0] = new UUID(0x1106);

agent.searchServices(att,uuids,device,this);
}catch(Exception e){
e.printStackTrace();}
}
//__________________________________________________ _______________________________
public void servicesDiscovered(int id,javax.bluetooth.ServiceRecord[] sr)
{
serviceR=sr;
//synchronized(servicese)
}
//__________________________________________________ _______________________________
public void inquiryCompleted(int params)
{
result=new String[devices.size()];
for(int i=0;i<devices.size();i++)
{
RemoteDevice rd=(RemoteDevice)devices.elementAt(i);
try {
result[i]=rd.getFriendlyName(true);
} catch (Exception e) {
}
}
repaint();
}

public void serviceSearchCompleted(int transID,int respCode)
{
// chk=true;
switch(respCode)
{
case DiscoveryListener.SERVICE_SEARCH_COMPLETED:
chk=1;
break;
case DiscoveryListener.SERVICE_SEARCH_ERROR:
chk=2;
break;
}
repaint();
}
//__________________________________________________ _______________________________
public void paint(Graphics g)
{
g.setColor(0xffffff);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0x0);
Font fo=Font.getDefaultFont();
String stt="";
int y=0;
if(devices!=null && chk==0)
for (int i = 0; i < devices.size(); i++)
{
if(i==current)
{
g.setColor(0x0);
g.drawString(result[i], 0, y, Graphics.BASELINE|Graphics.LEFT);
g.setColor(0x00ff00);
g.fillRect(0, y, getWidth(),fo.getHeight());
}
else
{
g.setColor(0x0);
g.drawString(result[i], 0, y, Graphics.BASELINE|Graphics.LEFT);
}
y+=fo.getHeight();
}

if(chk==1)
{
g.setColor(0xffffff);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0x0);
y=0;
for(int i=0;i<serviceR.length;i++)
{
DataElement d = serviceR[i].getAttributeValue(0x0100);
stt= serviceR[i].getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOE NCRYPT, false);
stt=(String)d.getValue()+"\n";
}
g.drawString(stt, 0, y+=20, Graphics.BASELINE|Graphics.LEFT);
URL=stt;
g.drawString("url"+URL, 0, y+=20, Graphics.BASELINE|Graphics.LEFT);
sendData(URL);
// try {
//
// new Thread(new Runnable() {
// public void run() {
// sendData(URL);
// }
// }).start();
//
// } catch (Exception e) {
// e.printStackTrace();
// }
}
else if(chk==2)
{
g.setColor(0xffffff);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0x0);
y=20;
g.drawString("Error", getWidth()/2, y,g.BASELINE|g.LEFT);

}



}
//************************************************** ****************************
public void keyPressed(int keycode)
{
int gameAct=getGameAction(keycode);
switch(gameAct)
{
case UP:
current--;
break;
case DOWN:
current++;
break;
case FIRE:
FindServices((RemoteDevice)devices.elementAt(curre nt));
break;
}
repaint();
}
//************************************************** ****************************


}