I'm trying to create an adhoc network but whenever I execute my script I'm getting an error saying:
"The operation couldn’t be completed. (com.apple.wifi.apple80211API.error error -3900.)"
My code looks like this:
import Foundation
import CoreWLAN;
import ObjectiveC;
var ssid = NSData();
var security = CWIBSSModeSecurity.None;
var channel = Int(11);
var password = NSString();
var error: NSError?
var iface = CWWiFiClient.sharedWiFiClient().interface();
println(iface.interfaceName);
var ibss = iface.startIBSSModeWithSSID(nil, security: CWIBSSModeSecurity.WEP104, channel: 11, password: password as String, error: &error);
if error != nil{
println(error?.localizedDescription);
}
var loop = NSRunLoop();
NSRunLoop.currentRunLoop().run();
Any idea as to what I'm doing wrong ?
Well, there's nothing of interest in the system log, so I tried running your code. I got the same error but, after poking around a bit, I managed to get things working. Here's what I ended up with.
import Foundation
import CoreWLAN
var error: NSError?
let iface = CWWiFiClient.sharedWiFiClient().interface()
println(iface.interfaceName)
let success = iface.startIBSSModeWithSSID(
"Test".dataUsingEncoding(NSUTF8StringEncoding),
security: CWIBSSModeSecurity.WEP104,
channel: 11,
password: "HelloCruelWorld",
error: &error
)
if !success {
println(error?.localizedDescription)
} else {
NSRunLoop.currentRunLoop().run()
}
With reference to your line numbers:
You have a lot of unnecessary semicolons (-:
Line 3 is a bit weird and definitely unnecessary.
Line 5 is unneeded because you never reference
.ssid
Likewise for
on line 6.security
Likewise for
on line 7.channel
Line 8 is definitely a problem; you can't create a
network without supplying a password..WEP104
In lines 11 and 14, you should use
notlet
because you don't change the values afterwards.var
In line 14 I changed
toibss
, which more accurately reflects its role.success
In line 16, Cocoa convention says that you shouldn't test
without first testing success (fortunately this class of problem will go away with Swift 2's shiny new error handling mechanism).error
Line 20 is unnecessary.
None of this actually fixed the problem )-:
After some more poking around I found that the issue was the SSID parameter. It seems that, despite what it says in the header comments,
-startIBSSModeWithSSID:xxx
really doesn't like nil here. Once I started passing in a real name, things just worked.
Please file a bug against the header commons, then post your bug number here, just for the record.
Share and Enjoy
—
Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"