当前位置:首页 » 编程软件 » 安卓蓝牙编程

安卓蓝牙编程

发布时间: 2024-10-20 22:23:32

① android蓝牙编程-如何实现自动连接上次连接过的蓝牙从机模块

每次只连接一个,传完数据后,连接下一个.要多连接的话,直接创建无线网络来连接.蓝牙能连接7个是因为只能保存蓝牙只能保存匹配数是7个

② 编程软件哪个好


对于"编程软件哪个好"这个问题感兴趣的朋友应该很多,这个也是目前大家比较关注的问题,那么下面小好小编就收集了一些"编程软件哪个好"相关软件,来分享给大家希望能够帮助到你哦。
1. Firebird(编程软件) 软件类型:电脑软件
软件介绍:
Firebird很容易让您从单用户,单数据库升级到企业级的应用。一个firebird数据库服务器能够管理多个独立的数据库,每一个数据库同时可支持多个客户端连结。
2. microbit编程软件 软件类型:安卓APP
软件介绍:
microbit手机上编程app是特别实用的少儿编程手机软件,特别适合新手来应用,可以利用手机蓝牙手机传输程序流程,适用可视化编程并让智能机器人实行这种命令,钟爱的朋友们赶紧在线下载畅玩吧!
3. sCode(编程软件) 软件类型:安卓APP
软件介绍:
sCode手机安卓版是一款变为学习程序编写的客户构建的学习助手,里边为我们梳理了一系列程序编写常见的编码,便捷使用者应用的并且可以使你轻轻松松把握大量的程序编写常见专业知识,一边学习,一边应用,必须的
4. 积木编程软件 软件类型:安卓APP
软件介绍:
积木编程手机软件是可以线上可以学习培训编程的手机软件,服务平台给客户带来了很多的编程学习培训实例教程,手机软件还适用线上编程,学习培训形式多样,让客户了解的时候不容易枯燥无味,即使你是零基础的新手,还
5. 儿童编程软件 软件类型:电脑软件
点击下载
软件介绍:
Scratch 是一个胡困禅专业对于小孩开发设计的数控编程软件,根据栩栩如生一目裤尘了然的乐高积木方式,能够增强小朋友们的程序编写快乐尺睁,早日变成程序编写大神。

③ android 蓝牙开发,在使用APP inventor编程后连接的时候出现了蓝牙权限问题,该怎么解决啊。急啊。

每个App里面都有一个Androidmanifest.xml文件,我最近也遇到这个问题。摸索了好久才明白的。在你用App Inventor生成App以后,你可以用Apptool之类的工具把App解压,里面就有一个Androidmanifest.xml文件,你打开以后添加你需要允许的权限,保存。再用Apptool反编译一下,反编译后的App要经过签名才能在android上安装,不然在大部分情况下的都是装不上的。像你这个情况的话直接在里面适当的位置添加<uses-permissionandroid:name="android.permission.BLUETOOTH_ADMIN" />就可以了

④ c++中蓝牙编程的库类

安装PSDK就可以用了,之前有写过一个类在MFC里面调用,并能成功与蓝牙手机通信,只不过是非标准的蓝牙协议。代码如下:
=====================h头文件========================

#ifndef NS_BTH
#include "ws2bth.h" //如果没有定义NS_BTH宏,则将PSDK头文件包含进来
#endif

#ifndef BTH_ADDR
typedef ULONGLONG BTH_ADDR;
#endif

#ifndef SAFE_DELETE
#define SAFE_DELETE(pObj) {if(pObj) delete pObj; pObj = NULL;}
#endif

#ifndef SAFE_DELETERG
#define SAFE_DELETERG(pObj) {if(pObj) delete [] pObj; pObj = NULL;}
#endif

#define RECEIVE_OVER_COMMAND 0x00001000

typedef struct _tag_BthDev_Info
{
BTH_ADDR btAddr;
TCHAR szAddr[32];
TCHAR szName[128];

_tag_BthDev_Info()
{
memset(this, 0, sizeof(this));
}
}
BTHDEV_INFO;

class CGGBlueTooth
{
public:
CGGBlueTooth();
~CGGBlueTooth();

BOOL StartSearchBthDev(IN DWORD dwControlFlags);
BOOL GetNextBthDev(
IN OUT BTHDEV_INFO *pcBthDevInfo,
IN DWORD dwControlFlags = LUP_RETURN_NAME | LUP_RETURN_ADDR | LUP_FLUSHCACHE
);
BOOL EndSearchBthDev();

//Socket Api
BOOL Create(int af = AF_BTH, int type = SOCK_STREAM, int protocol = BTHPROTO_RFCOMM);
BOOL Close();

//If no error occurs, this function returns zero. If an error occurs, it returns SOCKET_ERROR
int Bind(BTH_ADDR sktAddr = 0, ULONG port = BT_PORT_ANY);
int Listen(IN int backlog);
int GetSockName(BTH_ADDR *psktAddr, ULONG *port);
int SetBthService(TCHAR *lpServiceName);
int Accept(CGGBlueTooth *pcBth);
int Connect(BTH_ADDR sktAddr, ULONG port, int nMSecond = -1);
int Send(LPVOID lpData, int len, int flags = 0);
int Recv(LPVOID lpData, int len, int flags = 0);
int SendAll(LPVOID lpData, int len, int flags = 0);
int RecvAll(LPVOID lpData, int len, int flags = 0);

private:
void HexMac2String(BTH_ADDR dw64Addr, TCHAR *pstrMac);

private:
HANDLE m_hLookup;
SOCKET m_hSocket;
SOCKADDR_BTH m_sktAddr;
};

==============cpp文件================

#include "stdafx.h"
#include "GGBlueTooth.h"
//#include "tchar.h"

CGGBlueTooth::CGGBlueTooth()
: m_hLookup(NULL)
, m_hSocket(NULL)
{
memset(&m_sktAddr, 0, sizeof(m_sktAddr));
}

CGGBlueTooth::~CGGBlueTooth()
{
EndSearchBthDev();
Close();
}

void CGGBlueTooth::HexMac2String(BTH_ADDR dw64Addr, TCHAR *pstrMac)
{
BYTE *pbAddr = (BYTE*)&dw64Addr;

_stprintf(
pstrMac, _T("%02X:%02X:%02X:%02X:%02X:%02X"),
pbAddr[5], pbAddr[4], pbAddr[3],
pbAddr[2], pbAddr[1], pbAddr[0]
);
}

BOOL CGGBlueTooth::StartSearchBthDev(IN DWORD dwControlFlags)
{
WSAQUERYSET wsaq;
ZeroMemory(&wsaq, sizeof(wsaq));
wsaq.dwSize = sizeof(wsaq);
wsaq.dwNameSpace = NS_BTH;
wsaq.lpcsaBuffer = NULL;

return WSALookupServiceBegin(&wsaq, dwControlFlags, &m_hLookup) == ERROR_SUCCESS ? TRUE : FALSE;
}

BOOL CGGBlueTooth::GetNextBthDev(
IN OUT BTHDEV_INFO *pcBthDevInfo,
IN DWORD dwControlFlags /* = LUP_RETURN_NAME | LUP_RETURN_ADDR | LUP_FLUSHCACHE*/
)
{
if (!m_hLookup || !pcBthDevInfo)
{
return FALSE;
}

memset(pcBthDevInfo->szAddr, 0, sizeof(pcBthDevInfo->szAddr));
memset(pcBthDevInfo->szName, 0, sizeof(pcBthDevInfo->szName));

union
{
CHAR buf[5000];
double __unused; // ensure proper alignment
};
LPWSAQUERYSET pwsaResults = (LPWSAQUERYSET)buf;
DWORD dwSize = sizeof(buf);
int nResult;

ZeroMemory(pwsaResults, sizeof(WSAQUERYSET));
pwsaResults->dwSize = sizeof(WSAQUERYSET);
pwsaResults->dwNameSpace = NS_BTH;
pwsaResults->lpBlob = NULL;

nResult = WSALookupServiceNext (m_hLookup, dwControlFlags, &dwSize, pwsaResults);
if (nResult == ERROR_SUCCESS)
{
pcBthDevInfo->btAddr = ((SOCKADDR_BTH *)pwsaResults->lpcsaBuffer->RemoteAddr.lpSockaddr)->btAddr;
BOOL bHaveName = pwsaResults->lpszServiceInstanceName && *(pwsaResults->lpszServiceInstanceName);

if (bHaveName)
{
HexMac2String(pcBthDevInfo->btAddr, pcBthDevInfo->szAddr);
_tcscpy(pcBthDevInfo->szName, pwsaResults->lpszServiceInstanceName);
}
return TRUE;
}

return FALSE;
}

BOOL CGGBlueTooth::EndSearchBthDev()
{
if (m_hLookup)
{
WSALookupServiceEnd(m_hLookup);
m_hLookup = NULL;
return TRUE;
}

return FALSE;
}

///===============================

BOOL CGGBlueTooth::Create(int af/* = AF_BTH*/, int type/* = SOCK_STREAM*/, int protocol/* = BTHPROTO_RFCOMM*/)
{
if (m_hSocket)
{
return FALSE;
}

m_hSocket = socket(af, type, protocol);
m_sktAddr.addressFamily = af;
m_sktAddr.serviceClassId = GUID_NULL;
return m_hSocket == INVALID_SOCKET ? FALSE : TRUE;
}

BOOL CGGBlueTooth::Close()
{
if (m_hSocket)
{
closesocket(m_hSocket);
return TRUE;
}
return FALSE;
}

int CGGBlueTooth::Bind(BTH_ADDR sktAddr/* = 0*/, ULONG port/* = BT_PORT_ANY*/)
{
m_sktAddr.btAddr = sktAddr;
m_sktAddr.port = port;
return bind(m_hSocket, (SOCKADDR *)&m_sktAddr, sizeof(m_sktAddr));
}

int CGGBlueTooth::Listen(int backlog)
{
return listen(m_hSocket, backlog);
}

int CGGBlueTooth::GetSockName(BTH_ADDR *psktAddr, ULONG *port)
{
int nLen = sizeof(m_sktAddr);
int nResult = getsockname(m_hSocket, (SOCKADDR *)&m_sktAddr, &nLen);
if (nResult == 0)
{
*psktAddr = m_sktAddr.btAddr;
*port = m_sktAddr.port;
}
return nResult;
}

GUID OBEXFileTransferServiceClass_UUID_EX = {0x00001106, 0x0000, 0x1000, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB};
int CGGBlueTooth::SetBthService(TCHAR * lpServiceName)
{
CSADDR_INFO sockInfo;
sockInfo.iProtocol = BTHPROTO_RFCOMM;
sockInfo.iSocketType = SOCK_STREAM;
sockInfo.LocalAddr.lpSockaddr = (SOCKADDR *)&m_sktAddr;
sockInfo.LocalAddr.iSockaddrLength = sizeof(m_sktAddr);
sockInfo.RemoteAddr.lpSockaddr = (SOCKADDR *)&m_sktAddr;
sockInfo.RemoteAddr.iSockaddrLength = sizeof(m_sktAddr);

WSAQUERYSET svcInfo = {0};
svcInfo.dwSize = sizeof(svcInfo);
svcInfo.dwNameSpace = NS_BTH;
svcInfo.lpszServiceInstanceName = lpServiceName;
svcInfo.lpServiceClassId = &OBEXFileTransferServiceClass_UUID_EX;
svcInfo.dwNumberOfCsAddrs = 1;
svcInfo.lpcsaBuffer = &sockInfo;

return WSASetService(&svcInfo, RNRSERVICE_REGISTER, 0);
}

int CGGBlueTooth::Accept(CGGBlueTooth *pcBth)
{
SOCKADDR_BTH ca;
int size = sizeof(ca);
pcBth->m_hSocket = accept (m_hSocket, (SOCKADDR *)&ca, &size);
pcBth->m_sktAddr = ca;
return pcBth->m_hSocket == INVALID_SOCKET ? SOCKET_ERROR : 0;
}

int CGGBlueTooth::Connect(BTH_ADDR sktAddr, ULONG port, int nMSecond/* = -1*/)
{
SOCKADDR_BTH sa = {0};
sa.addressFamily = AF_BTH;
sa.btAddr = sktAddr;
sa.port = port;

if (nMSecond == -1)
{
return connect(m_hSocket, (LPSOCKADDR)&sa, sizeof(SOCKADDR_BTH));
}

ULONG non_blocking = 1;
ULONG blocking = 0;

int nResult = ioctlsocket(m_hSocket, FIONBIO, &non_blocking);
if (nResult == SOCKET_ERROR)
{
return nResult;
}

nResult = SOCKET_ERROR;
if (connect(m_hSocket, (LPSOCKADDR)&sa, sizeof(SOCKADDR_BTH)) == SOCKET_ERROR)
{
struct timeval tv;
fd_set writefds;

// 设置连接超时时间
tv.tv_sec = nMSecond / 1000; // 秒数
tv.tv_usec = nMSecond % 1000; // 毫秒

FD_ZERO(&writefds);
FD_SET(m_hSocket, &writefds);

nResult = select((int)m_hSocket + 1, NULL, &writefds, NULL, &tv);
if(nResult > 0)
{
if(FD_ISSET(m_hSocket, &writefds))
{
int error = 0;
int len = sizeof(error);
//下面的一句一定要,主要针对防火墙
if(!(getsockopt(m_hSocket, SOL_SOCKET, SO_ERROR, (char *)&error, &len) != 0 || error != 0))
{
nResult = 0;
}
}
}
else if (nResult == 0)
{
nResult = -2;
}
}

if (ioctlsocket(m_hSocket, FIONBIO, &blocking) == SOCKET_ERROR)
{
nResult = SOCKET_ERROR;
}
return nResult;
}

int CGGBlueTooth::Send(LPVOID lpData, int len, int flags/* = 0*/)
{
return send(m_hSocket, (char *)lpData, len, flags);
}

int CGGBlueTooth::Recv(LPVOID lpData, int len, int flags/* = 0*/)
{
return recv(m_hSocket, (char *)lpData, len, flags);
}

int CGGBlueTooth::SendAll(LPVOID lpData, int len, int flags/* = 0*/)
{
int nCount = send(m_hSocket, (char *)lpData, len, flags);
if (nCount == SOCKET_ERROR && WSAGetLastError() != WSAEWOULDBLOCK)
{
return SOCKET_ERROR;
}

int nCommand = 0;
nCount = recv(m_hSocket, (char *)&nCommand, 4, flags);
if (nCount != 4 || nCommand != RECEIVE_OVER_COMMAND)
{
return SOCKET_ERROR;
}

return ERROR_SUCCESS;
}

int CGGBlueTooth::RecvAll(LPVOID lpData, int len, int flags/* = 0*/)
{
int nCount = -1, nCurRecv = 0, nMaxRead = 32 * 1024;

while (nCurRecv < len)
{
if (len - nCurRecv < nMaxRead)
{
nMaxRead = len - nCurRecv;
}

nCount = recv(m_hSocket, (char *)lpData + nCurRecv, nMaxRead, flags);

if (nCount == SOCKET_ERROR)
{
if (WSAGetLastError() == WSAEWOULDBLOCK)
{
Sleep(1);
continue;
}
else
{
return SOCKET_ERROR;
}
}

nCurRecv += nCount;
}

int nCommand = RECEIVE_OVER_COMMAND;
nCount = send(m_hSocket, (char *)&nCommand, 4, flags);
if (nCount == SOCKET_ERROR && WSAGetLastError() != WSAEWOULDBLOCK)
{
return SOCKET_ERROR;
}

return ERROR_SUCCESS;
}

⑤ android多连接蓝牙编程,据说可以同时进行7个连接

你把你的代码贴出来,在接收数据的时候是数据流接收,需要循环接收。

热点内容
安卓71的n是什么 发布:2024-11-24 00:27:27 浏览:389
存储一个国际码需要几个字节 发布:2024-11-24 00:26:41 浏览:957
javalistadd 发布:2024-11-24 00:26:39 浏览:428
fgo脚本登录 发布:2024-11-24 00:20:29 浏览:18
在dos下如何查看配置 发布:2024-11-24 00:20:28 浏览:731
北京时间服务器在什么地方 发布:2024-11-24 00:19:50 浏览:18
分贝的算法 发布:2024-11-24 00:16:07 浏览:928
横截面算法 发布:2024-11-24 00:15:12 浏览:258
pythontile 发布:2024-11-24 00:10:04 浏览:546
修改pdf加密文件 发布:2024-11-24 00:09:50 浏览:223